diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-12-15 23:05:30 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-12-15 23:05:30 +1100 |
| commit | f53292f7ee7365fe50ac216efac438ff5569fd06 (patch) | |
| tree | 269b181beb06c1701eff224eaecfc1e4dd6d793d | |
| parent | 4f62c969f618463914b148d53bef1d0faeb2782f (diff) | |
| download | rust-f53292f7ee7365fe50ac216efac438ff5569fd06.tar.gz rust-f53292f7ee7365fe50ac216efac438ff5569fd06.zip | |
Move std::{str,vec}::raw::set_len to an unsafe method on Owned{Vector,Str}.
| -rw-r--r-- | doc/po/ja/tutorial-ffi.md.po | 4 | ||||
| -rw-r--r-- | doc/po/tutorial-ffi.md.pot | 4 | ||||
| -rw-r--r-- | doc/tutorial-ffi.md | 4 | ||||
| -rw-r--r-- | src/librustuv/process.rs | 2 | ||||
| -rw-r--r-- | src/libstd/io/buffered.rs | 4 | ||||
| -rw-r--r-- | src/libstd/io/mod.rs | 4 | ||||
| -rw-r--r-- | src/libstd/io/native/file.rs | 2 | ||||
| -rw-r--r-- | src/libstd/os.rs | 4 | ||||
| -rw-r--r-- | src/libstd/rt/stack.rs | 2 | ||||
| -rw-r--r-- | src/libstd/str.rs | 38 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 66 |
11 files changed, 68 insertions, 66 deletions
diff --git a/doc/po/ja/tutorial-ffi.md.po b/doc/po/ja/tutorial-ffi.md.po index ff28357cabf..c5df23b3971 100644 --- a/doc/po/ja/tutorial-ffi.md.po +++ b/doc/po/ja/tutorial-ffi.md.po @@ -224,7 +224,7 @@ msgstr "" #, no-wrap msgid "" " snappy_compress(psrc, srclen, pdst, &mut dstlen);\n" -" vec::raw::set_len(&mut dst, dstlen as uint);\n" +" dst.set_len(dstlen as uint);\n" " dst\n" " }\n" "}\n" @@ -271,7 +271,7 @@ msgstr "" #, no-wrap msgid "" " if snappy_uncompress(psrc, srclen, pdst, &mut dstlen) == 0 {\n" -" vec::raw::set_len(&mut dst, dstlen as uint);\n" +" dst.set_len(dstlen as uint);\n" " Some(dst)\n" " } else {\n" " None // SNAPPY_INVALID_INPUT\n" diff --git a/doc/po/tutorial-ffi.md.pot b/doc/po/tutorial-ffi.md.pot index c3116dea872..f5a29fd59c3 100644 --- a/doc/po/tutorial-ffi.md.pot +++ b/doc/po/tutorial-ffi.md.pot @@ -224,7 +224,7 @@ msgstr "" #, no-wrap msgid "" " snappy_compress(psrc, srclen, pdst, &mut dstlen);\n" -" vec::raw::set_len(&mut dst, dstlen as uint);\n" +" dst.set_len(dstlen as uint);\n" " dst\n" " }\n" "}\n" @@ -271,7 +271,7 @@ msgstr "" #, no-wrap msgid "" " if snappy_uncompress(psrc, srclen, pdst, &mut dstlen) == 0 {\n" -" vec::raw::set_len(&mut dst, dstlen as uint);\n" +" dst.set_len(dstlen as uint);\n" " Some(dst)\n" " } else {\n" " None // SNAPPY_INVALID_INPUT\n" diff --git a/doc/tutorial-ffi.md b/doc/tutorial-ffi.md index 0746728a0f5..7ff82ffcadb 100644 --- a/doc/tutorial-ffi.md +++ b/doc/tutorial-ffi.md @@ -107,7 +107,7 @@ pub fn compress(src: &[u8]) -> ~[u8] { let pdst = vec::raw::to_mut_ptr(dst); snappy_compress(psrc, srclen, pdst, &mut dstlen); - vec::raw::set_len(&mut dst, dstlen as uint); + dst.set_len(dstlen as uint); dst } } @@ -129,7 +129,7 @@ pub fn uncompress(src: &[u8]) -> Option<~[u8]> { let pdst = vec::raw::to_mut_ptr(dst); if snappy_uncompress(psrc, srclen, pdst, &mut dstlen) == 0 { - vec::raw::set_len(&mut dst, dstlen as uint); + dst.set_len(dstlen as uint); Some(dst) } else { None // SNAPPY_INVALID_INPUT diff --git a/src/librustuv/process.rs b/src/librustuv/process.rs index 8098ea653bc..0410d58bd8b 100644 --- a/src/librustuv/process.rs +++ b/src/librustuv/process.rs @@ -49,7 +49,7 @@ impl Process { let mut stdio = vec::with_capacity::<uvll::uv_stdio_container_t>(io.len()); let mut ret_io = vec::with_capacity(io.len()); unsafe { - vec::raw::set_len(&mut stdio, io.len()); + stdio.set_len(io.len()); for (slot, other) in stdio.iter().zip(io.iter()) { let io = set_stdio(slot as *uvll::uv_stdio_container_t, other, loop_); diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index 1922af3a3b7..fb06a4dfdb4 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -79,7 +79,7 @@ impl<R: Reader> BufferedReader<R> { // to be very cheap (large mallocs are not nearly as expensive as large // callocs). let mut buf = vec::with_capacity(cap); - unsafe { vec::raw::set_len(&mut buf, cap); } + unsafe { buf.set_len(cap); } BufferedReader { inner: inner, buf: buf, @@ -154,7 +154,7 @@ impl<W: Writer> BufferedWriter<W> { pub fn with_capacity(cap: uint, inner: W) -> BufferedWriter<W> { // See comments in BufferedReader for why this uses unsafe code. let mut buf = vec::with_capacity(cap); - unsafe { vec::raw::set_len(&mut buf, cap); } + unsafe { buf.set_len(cap); } BufferedWriter { inner: inner, buf: buf, diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index c5ae6457515..53d79e70c86 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -523,7 +523,7 @@ pub trait Reader { let mut total_read = 0; buf.reserve_additional(len); - vec::raw::set_len(buf, start_len + len); + buf.set_len(start_len + len); (|| { while total_read < len { @@ -539,7 +539,7 @@ pub trait Reader { } } } - }).finally(|| vec::raw::set_len(buf, start_len + total_read)) + }).finally(|| buf.set_len(start_len + total_read)) } } diff --git a/src/libstd/io/native/file.rs b/src/libstd/io/native/file.rs index 7b5104657d9..4e1c6bf982a 100644 --- a/src/libstd/io/native/file.rs +++ b/src/libstd/io/native/file.rs @@ -706,7 +706,7 @@ pub fn readlink(p: &CString) -> IoResult<Path> { -1 => Err(super::last_error()), n => { assert!(n > 0); - unsafe { vec::raw::set_len(&mut buf, n as uint); } + unsafe { buf.set_len(n as uint); } Ok(Path::new(buf)) } } diff --git a/src/libstd/os.rs b/src/libstd/os.rs index bcd353bab7a..f82df73908c 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -369,7 +369,7 @@ pub fn self_exe_path() -> Option<Path> { }); if err != 0 { return None; } if sz == 0 { return None; } - vec::raw::set_len(&mut v, sz as uint - 1); // chop off trailing NUL + v.set_len(sz as uint - 1); // chop off trailing NUL Some(v) } } @@ -398,7 +398,7 @@ pub fn self_exe_path() -> Option<Path> { _NSGetExecutablePath(buf as *mut i8, &mut sz) }); if err != 0 { return None; } - vec::raw::set_len(&mut v, sz as uint - 1); // chop off trailing NUL + v.set_len(sz as uint - 1); // chop off trailing NUL Some(v) } } diff --git a/src/libstd/rt/stack.rs b/src/libstd/rt/stack.rs index 4358390da9f..4b3236b76bf 100644 --- a/src/libstd/rt/stack.rs +++ b/src/libstd/rt/stack.rs @@ -24,7 +24,7 @@ impl StackSegment { unsafe { // Crate a block of uninitialized values let mut stack = vec::with_capacity(size); - vec::raw::set_len(&mut stack, size); + stack.set_len(size); let mut stk = StackSegment { buf: stack, diff --git a/src/libstd/str.rs b/src/libstd/str.rs index af381ef3cf0..5a7f6f5dc3f 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -993,7 +993,7 @@ pub mod raw { use cast; use libc; use ptr; - use str::is_utf8; + use str::{is_utf8, OwnedStr}; use vec; use vec::MutableVector; use unstable::raw::Slice; @@ -1002,7 +1002,7 @@ pub mod raw { pub unsafe fn from_buf_len(buf: *u8, len: uint) -> ~str { let mut v: ~[u8] = vec::with_capacity(len); v.as_mut_buf(|vbuf, _len| ptr::copy_memory(vbuf, buf as *u8, len)); - vec::raw::set_len(&mut v, len); + v.set_len(len); assert!(is_utf8(v)); ::cast::transmute(v) @@ -1109,7 +1109,7 @@ pub mod raw { let len = s.len(); assert!((len > 0u)); let b = s[len - 1u]; - set_len(s, len - 1u); + s.set_len(len - 1); return b; } @@ -1133,16 +1133,6 @@ pub mod raw { /// Sets the length of a string /// /// This will explicitly set the size of the string, without actually - /// modifying its buffers, so it is up to the caller to ensure that - /// the string is actually the specified size. - #[inline] - pub unsafe fn set_len(s: &mut ~str, new_len: uint) { - vec::raw::set_len(as_owned_vec(s), new_len) - } - - /// Sets the length of a string - /// - /// This will explicitly set the size of the string, without actually /// modifing its buffers, so it is up to the caller to ensure that /// the string is actually the specified size. #[test] @@ -1339,7 +1329,7 @@ impl Mutable for ~str { #[inline] fn clear(&mut self) { unsafe { - raw::set_len(self, 0) + self.set_len(0) } } } @@ -2293,7 +2283,7 @@ impl<'a> StrSlice<'a> for &'a str { let mut v = vec::with_capacity(len); v.as_mut_buf(|dst, _| ptr::copy_memory(dst, src, len)); - vec::raw::set_len(&mut v, len); + v.set_len(len); ::cast::transmute(v) } }) @@ -2598,6 +2588,13 @@ pub trait OwnedStr { /// The caller must make sure any mutations to this buffer keep the string /// valid UTF-8! fn as_mut_buf<T>(&mut self, f: |*mut u8, uint| -> T) -> T; + + /// Sets the length of a string + /// + /// This will explicitly set the size of the string, without actually + /// modifying its buffers, so it is up to the caller to ensure that + /// the string is actually the specified size. + unsafe fn set_len(&mut self, new_len: uint); } impl OwnedStr for ~str { @@ -2629,7 +2626,7 @@ impl OwnedStr for ~str { c.encode_utf8(slc) }) }); - raw::set_len(self, cur_len + used); + self.set_len(cur_len + used); } } @@ -2638,7 +2635,7 @@ impl OwnedStr for ~str { let end = self.len(); assert!(end > 0u); let CharRange {ch, next} = self.char_range_at_reverse(end); - unsafe { raw::set_len(self, next); } + unsafe { self.set_len(next); } return ch; } @@ -2689,7 +2686,7 @@ impl OwnedStr for ~str { fn truncate(&mut self, len: uint) { assert!(len <= self.len()); assert!(self.is_char_boundary(len)); - unsafe { raw::set_len(self, len); } + unsafe { self.set_len(len); } } #[inline] @@ -2703,6 +2700,11 @@ impl OwnedStr for ~str { raw::as_owned_vec(self).as_mut_buf(f) } } + + #[inline] + unsafe fn set_len(&mut self, new_len: uint) { + raw::as_owned_vec(self).set_len(new_len) + } } impl Clone for ~str { diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index fc051a7e2b5..9919c552346 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -143,7 +143,7 @@ pub fn from_fn<T>(n_elts: uint, op: |uint| -> T) -> ~[T] { i += 1u; } }).finally(|| { - raw::set_len(&mut v, i); + v.set_len(i); }); v } @@ -170,7 +170,7 @@ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> ~[T] { i += 1u; } }).finally(|| { - raw::set_len(&mut v, i); + v.set_len(i); }); v } @@ -1440,6 +1440,15 @@ pub trait OwnedVector<T> { * value */ fn grow_fn(&mut self, n: uint, op: |uint| -> T); + + /** + * Sets the length of a vector + * + * This will explicitly set the size of the vector, without actually + * modifying its buffers, so it is up to the caller to ensure that + * the vector is actually the specified size. + */ + unsafe fn set_len(&mut self, new_len: uint); } impl<T> OwnedVector<T> for ~[T] { @@ -1565,8 +1574,8 @@ impl<T> OwnedVector<T> for ~[T] { let self_p = vec::raw::to_mut_ptr(*self); let rhs_p = vec::raw::to_ptr(rhs); ptr::copy_memory(ptr::mut_offset(self_p, self_len as int), rhs_p, rhs_len); - raw::set_len(self, new_len); - raw::set_len(&mut rhs, 0); + self.set_len(new_len); + rhs.set_len(0); } } @@ -1576,7 +1585,7 @@ impl<T> OwnedVector<T> for ~[T] { ln => { let valptr = ptr::to_mut_unsafe_ptr(&mut self[ln - 1u]); unsafe { - raw::set_len(self, ln - 1u); + self.set_len(ln - 1u); Some(ptr::read_ptr(&*valptr)) } } @@ -1616,7 +1625,7 @@ impl<T> OwnedVector<T> for ~[T] { assert!(self.capacity() >= ln); // Pretend like we have the original length so we can use // the vector copy_memory to overwrite the hole we just made - raw::set_len(self, ln); + self.set_len(ln); // Memcopy the head element (the one we want) to the location we just // popped. For the moment it unsafely exists at both the head and last @@ -1636,7 +1645,7 @@ impl<T> OwnedVector<T> for ~[T] { } // Set the new length. Now the vector is back to normal - raw::set_len(self, next_ln); + self.set_len(next_ln); // Swap out the element we want from the end let vp = raw::to_mut_ptr(*self); @@ -1692,7 +1701,7 @@ impl<T> OwnedVector<T> for ~[T] { } } }); - unsafe { raw::set_len(self, newlen); } + unsafe { self.set_len(newlen); } } fn retain(&mut self, f: |t: &T| -> bool) { @@ -1736,6 +1745,16 @@ impl<T> OwnedVector<T> for ~[T] { i += 1u; } } + #[inline] + unsafe fn set_len(&mut self, new_len: uint) { + if owns_managed::<T>() { + let repr: **mut Box<Vec<()>> = cast::transmute(self); + (**repr).data.fill = new_len * mem::nonzero_size_of::<T>(); + } else { + let repr: **mut Vec<()> = cast::transmute(self); + (**repr).fill = new_len * mem::nonzero_size_of::<T>(); + } + } } impl<T> Mutable for ~[T] { @@ -2190,26 +2209,7 @@ pub mod raw { use mem; use unstable::intrinsics; use vec::{with_capacity, ImmutableVector, MutableVector}; - use unstable::raw::{Box, Vec, Slice}; - use unstable::intrinsics::owns_managed; - - /** - * Sets the length of a vector - * - * This will explicitly set the size of the vector, without actually - * modifying its buffers, so it is up to the caller to ensure that - * the vector is actually the specified size. - */ - #[inline] - pub unsafe fn set_len<T>(v: &mut ~[T], new_len: uint) { - if owns_managed::<T>() { - let repr: **mut Box<Vec<()>> = cast::transmute(v); - (**repr).data.fill = new_len * mem::nonzero_size_of::<T>(); - } else { - let repr: **mut Vec<()> = cast::transmute(v); - (**repr).fill = new_len * mem::nonzero_size_of::<T>(); - } - } + use unstable::raw::Slice; /** * Returns an unsafe pointer to the vector's buffer @@ -2287,7 +2287,7 @@ pub mod raw { #[inline] pub unsafe fn from_buf_raw<T>(ptr: *T, elts: uint) -> ~[T] { let mut dst = with_capacity(elts); - set_len(&mut dst, elts); + dst.set_len(elts); dst.as_mut_buf(|p_dst, _len_dst| ptr::copy_memory(p_dst, ptr, elts)); dst } @@ -2379,7 +2379,7 @@ pub mod bytes { ptr::copy_memory(p_dst.offset(len_dst as int), p_src, len_src) }) }); - raw::set_len(dst, old_len + src.len()); + dst.set_len(old_len + src.len()); } } } @@ -4293,7 +4293,7 @@ mod bench { unsafe { let vp = vec::raw::to_mut_ptr(v); ptr::set_memory(vp, 0, 1024); - vec::raw::set_len(&mut v, 1024); + v.set_len(1024); } }); } @@ -4312,7 +4312,7 @@ mod bench { bh.iter(|| { let mut v: ~[u8] = vec::with_capacity(1024); unsafe { - vec::raw::set_len(&mut v, 1024); + v.set_len(1024); } for i in range(0, 1024) { v[i] = 0; @@ -4325,7 +4325,7 @@ mod bench { bh.iter(|| { let mut v: ~[u8] = vec::with_capacity(1024); unsafe { - vec::raw::set_len(&mut v, 1024); + v.set_len(1024); } for x in v.mut_iter() { *x = 0; |
