diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2014-12-18 22:16:48 -0500 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2014-12-19 03:29:59 -0500 |
| commit | a04ce71172d8b51aa0d7df288bfd7333e39c763a (patch) | |
| tree | 3495c43bd416cbd1d7e4c29f50f4a39c384f0691 /src | |
| parent | 89922e52b06fbe1af74983cf2f936c5bf9e6d91e (diff) | |
| download | rust-a04ce71172d8b51aa0d7df288bfd7333e39c763a.tar.gz rust-a04ce71172d8b51aa0d7df288bfd7333e39c763a.zip | |
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 #12677.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/str.rs | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 28110cf7b1a..8fe41c0bd89 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -21,6 +21,7 @@ pub use self::Searcher::{Naive, TwoWay, TwoWayLong}; 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 mem; 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 @@ impl<'a> DoubleEndedIterator<(uint, char)> for CharOffsets<'a> { /// 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 @@ impl StrPrelude for str { fn bytes(&self) -> Bytes { fn deref(&x: &u8) -> u8 { x } - self.as_bytes().iter().map(deref) + self.as_bytes().iter().map(BytesFn(deref)) } #[inline] |
