Skip to content

Commit a04ce71

Browse files
committed
Modify the Bytes type so that it remains cloneable even
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 rust-lang#12677.
1 parent 89922e5 commit a04ce71

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/libcore/str.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub use self::Searcher::{Naive, TwoWay, TwoWayLong};
2121

2222
use char::Char;
2323
use char;
24+
use clone::Clone;
2425
use cmp::{Eq, mod};
2526
use default::Default;
2627
use iter::{Map, Iterator, IteratorExt, DoubleEndedIterator};
@@ -31,7 +32,7 @@ use mem;
3132
use num::Int;
3233
use option::Option;
3334
use option::Option::{None, Some};
34-
use ops::FnMut;
35+
use ops::{Fn, FnMut};
3536
use ptr::RawPtr;
3637
use raw::{Repr, Slice};
3738
use slice::{mod, SliceExt};
@@ -316,7 +317,23 @@ impl<'a> DoubleEndedIterator<(uint, char)> for CharOffsets<'a> {
316317

317318
/// External iterator for a string's bytes.
318319
/// Use with the `std::iter` module.
319-
pub type Bytes<'a> = Map<&'a u8, u8, slice::Items<'a, u8>, fn(&u8) -> u8>;
320+
pub type Bytes<'a> = Map<&'a u8, u8, slice::Items<'a, u8>, BytesFn>;
321+
322+
/// A temporary new type wrapper that ensures that the `Bytes` iterator
323+
/// is cloneable.
324+
#[deriving(Copy)]
325+
#[experimental = "iterator type instability"]
326+
pub struct BytesFn(fn(&u8) -> u8);
327+
328+
impl<'a> Fn(&'a u8) -> u8 for BytesFn {
329+
extern "rust-call" fn call(&self, (ptr,): (&'a u8,)) -> u8 {
330+
(self.0)(ptr)
331+
}
332+
}
333+
334+
impl Clone for BytesFn {
335+
fn clone(&self) -> BytesFn { *self }
336+
}
320337

321338
/// An iterator over the substrings of a string, separated by `sep`.
322339
#[deriving(Clone)]
@@ -2009,7 +2026,7 @@ impl StrPrelude for str {
20092026
fn bytes(&self) -> Bytes {
20102027
fn deref(&x: &u8) -> u8 { x }
20112028

2012-
self.as_bytes().iter().map(deref)
2029+
self.as_bytes().iter().map(BytesFn(deref))
20132030
}
20142031

20152032
#[inline]

0 commit comments

Comments
 (0)