diff options
| author | bors <bors@rust-lang.org> | 2014-10-27 23:02:55 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-10-27 23:02:55 +0000 |
| commit | bd7138dd698dde29fb4d7fd34529a863b85d947e (patch) | |
| tree | b0d85f6266675501dce79b4802325e0d60b147e3 /src/libstd | |
| parent | e05c3b7799b45b78baf49f05763865be838f5b43 (diff) | |
| parent | 4dc06dceb2bbb7ced9ea137b5280f7ce413b4d01 (diff) | |
| download | rust-bd7138dd698dde29fb4d7fd34529a863b85d947e.tar.gz rust-bd7138dd698dde29fb4d7fd34529a863b85d947e.zip | |
auto merge of #18368 : alexcrichton/rust/rollup, r=alexcrichton
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/ascii.rs | 23 | ||||
| -rw-r--r-- | src/libstd/collections/hashmap/set.rs | 2 | ||||
| -rw-r--r-- | src/libstd/io/mem.rs | 9 |
3 files changed, 27 insertions, 7 deletions
diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index 07be15486fd..c2e88bfdbcf 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -247,8 +247,7 @@ impl OwnedAsciiCast for String { #[inline] unsafe fn into_ascii_nocheck(self) -> Vec<Ascii> { - let v: Vec<u8> = mem::transmute(self); - v.into_ascii_nocheck() + self.into_bytes().into_ascii_nocheck() } } @@ -260,7 +259,14 @@ impl OwnedAsciiCast for Vec<u8> { #[inline] unsafe fn into_ascii_nocheck(self) -> Vec<Ascii> { - mem::transmute(self) + let v = Vec::from_raw_parts(self.as_ptr() as *mut Ascii, + self.len(), + self.capacity()); + + // We forget `self` to avoid freeing it at the end of the scope + // Otherwise, the returned `Vec` would point to freed memory + mem::forget(self); + v } } @@ -338,7 +344,16 @@ pub trait IntoBytes { impl IntoBytes for Vec<Ascii> { fn into_bytes(self) -> Vec<u8> { - unsafe { mem::transmute(self) } + unsafe { + let v = Vec::from_raw_parts(self.as_ptr() as *mut u8, + self.len(), + self.capacity()); + + // We forget `self` to avoid freeing it at the end of the scope + // Otherwise, the returned `Vec` would point to freed memory + mem::forget(self); + v + } } } diff --git a/src/libstd/collections/hashmap/set.rs b/src/libstd/collections/hashmap/set.rs index dde1f27c9a3..ca954679c1c 100644 --- a/src/libstd/collections/hashmap/set.rs +++ b/src/libstd/collections/hashmap/set.rs @@ -186,7 +186,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> { /// # Example /// /// This is a slightly silly example where we define the number's - /// parity as the equivilance class. It is important that the + /// parity as the equivalance class. It is important that the /// values hash the same, which is why we implement `Hash`. /// /// ``` diff --git a/src/libstd/io/mem.rs b/src/libstd/io/mem.rs index f86ae05d623..dd4a3e05935 100644 --- a/src/libstd/io/mem.rs +++ b/src/libstd/io/mem.rs @@ -22,7 +22,7 @@ use slice; use slice::AsSlice; use vec::Vec; -static BUF_CAPACITY: uint = 128; +const BUF_CAPACITY: uint = 128; fn combine(seek: SeekStyle, cur: uint, end: uint, offset: i64) -> IoResult<u64> { // compute offset as signed and clamp to prevent overflow @@ -71,7 +71,12 @@ impl MemWriter { /// the internal buffer. #[inline] pub fn with_capacity(n: uint) -> MemWriter { - MemWriter { buf: Vec::with_capacity(n) } + MemWriter::from_vec(Vec::with_capacity(n)) + } + /// Create a new `MemWriter` that will append to an existing `Vec`. + #[inline] + pub fn from_vec(buf: Vec<u8>) -> MemWriter { + MemWriter { buf: buf } } /// Acquires an immutable reference to the underlying buffer of this |
