]> git.lizzy.rs Git - rust.git/commitdiff
Modify the `Bytes` type so that it remains cloneable even
authorNiko Matsakis <niko@alum.mit.edu>
Fri, 19 Dec 2014 03:16:48 +0000 (22:16 -0500)
committerNiko Matsakis <niko@alum.mit.edu>
Fri, 19 Dec 2014 08:29:59 +0000 (03:29 -0500)
though it includes a `fn()`. This is really a more general
problem but I wanted to ensures that `bytes` in particular
remains working due to #12677.

src/libcore/str.rs

index 28110cf7b1a374fc390fcb898f920d3e3941dae8..8fe41c0bd89c26e73b742cb047c38c9d54a7de26 100644 (file)
@@ -21,6 +21,7 @@
 
 use char::Char;
 use char;
+use clone::Clone;
 use cmp::{Eq, mod};
 use default::Default;
 use iter::{Map, Iterator, IteratorExt, DoubleEndedIterator};
@@ -31,7 +32,7 @@
 use num::Int;
 use option::Option;
 use option::Option::{None, Some};
-use ops::FnMut;
+use ops::{Fn, FnMut};
 use ptr::RawPtr;
 use raw::{Repr, Slice};
 use slice::{mod, SliceExt};
@@ -316,7 +317,23 @@ fn next_back(&mut self) -> Option<(uint, char)> {
 
 /// External iterator for a string's bytes.
 /// Use with the `std::iter` module.
-pub type Bytes<'a> = Map<&'a u8, u8, slice::Items<'a, u8>, fn(&u8) -> u8>;
+pub type Bytes<'a> = Map<&'a u8, u8, slice::Items<'a, u8>, BytesFn>;
+
+/// A temporary new type wrapper that ensures that the `Bytes` iterator
+/// is cloneable.
+#[deriving(Copy)]
+#[experimental = "iterator type instability"]
+pub struct BytesFn(fn(&u8) -> u8);
+
+impl<'a> Fn(&'a u8) -> u8 for BytesFn {
+    extern "rust-call" fn call(&self, (ptr,): (&'a u8,)) -> u8 {
+        (self.0)(ptr)
+    }
+}
+
+impl Clone for BytesFn {
+    fn clone(&self) -> BytesFn { *self }
+}
 
 /// An iterator over the substrings of a string, separated by `sep`.
 #[deriving(Clone)]
@@ -2009,7 +2026,7 @@ fn chars(&self) -> Chars {
     fn bytes(&self) -> Bytes {
         fn deref(&x: &u8) -> u8 { x }
 
-        self.as_bytes().iter().map(deref)
+        self.as_bytes().iter().map(BytesFn(deref))
     }
 
     #[inline]