Skip to content

Commit f53292f

Browse files
committed
Move std::{str,vec}::raw::set_len to an unsafe method on Owned{Vector,Str}.
1 parent 4f62c96 commit f53292f

File tree

11 files changed

+68
-66
lines changed

11 files changed

+68
-66
lines changed

doc/po/ja/tutorial-ffi.md.po

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ msgstr ""
224224
#, no-wrap
225225
msgid ""
226226
" snappy_compress(psrc, srclen, pdst, &mut dstlen);\n"
227-
" vec::raw::set_len(&mut dst, dstlen as uint);\n"
227+
" dst.set_len(dstlen as uint);\n"
228228
" dst\n"
229229
" }\n"
230230
"}\n"
@@ -271,7 +271,7 @@ msgstr ""
271271
#, no-wrap
272272
msgid ""
273273
" if snappy_uncompress(psrc, srclen, pdst, &mut dstlen) == 0 {\n"
274-
" vec::raw::set_len(&mut dst, dstlen as uint);\n"
274+
" dst.set_len(dstlen as uint);\n"
275275
" Some(dst)\n"
276276
" } else {\n"
277277
" None // SNAPPY_INVALID_INPUT\n"

doc/po/tutorial-ffi.md.pot

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ msgstr ""
224224
#, no-wrap
225225
msgid ""
226226
" snappy_compress(psrc, srclen, pdst, &mut dstlen);\n"
227-
" vec::raw::set_len(&mut dst, dstlen as uint);\n"
227+
" dst.set_len(dstlen as uint);\n"
228228
" dst\n"
229229
" }\n"
230230
"}\n"
@@ -271,7 +271,7 @@ msgstr ""
271271
#, no-wrap
272272
msgid ""
273273
" if snappy_uncompress(psrc, srclen, pdst, &mut dstlen) == 0 {\n"
274-
" vec::raw::set_len(&mut dst, dstlen as uint);\n"
274+
" dst.set_len(dstlen as uint);\n"
275275
" Some(dst)\n"
276276
" } else {\n"
277277
" None // SNAPPY_INVALID_INPUT\n"

doc/tutorial-ffi.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub fn compress(src: &[u8]) -> ~[u8] {
107107
let pdst = vec::raw::to_mut_ptr(dst);
108108
109109
snappy_compress(psrc, srclen, pdst, &mut dstlen);
110-
vec::raw::set_len(&mut dst, dstlen as uint);
110+
dst.set_len(dstlen as uint);
111111
dst
112112
}
113113
}
@@ -129,7 +129,7 @@ pub fn uncompress(src: &[u8]) -> Option<~[u8]> {
129129
let pdst = vec::raw::to_mut_ptr(dst);
130130
131131
if snappy_uncompress(psrc, srclen, pdst, &mut dstlen) == 0 {
132-
vec::raw::set_len(&mut dst, dstlen as uint);
132+
dst.set_len(dstlen as uint);
133133
Some(dst)
134134
} else {
135135
None // SNAPPY_INVALID_INPUT

src/librustuv/process.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Process {
4949
let mut stdio = vec::with_capacity::<uvll::uv_stdio_container_t>(io.len());
5050
let mut ret_io = vec::with_capacity(io.len());
5151
unsafe {
52-
vec::raw::set_len(&mut stdio, io.len());
52+
stdio.set_len(io.len());
5353
for (slot, other) in stdio.iter().zip(io.iter()) {
5454
let io = set_stdio(slot as *uvll::uv_stdio_container_t, other,
5555
loop_);

src/libstd/io/buffered.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<R: Reader> BufferedReader<R> {
7979
// to be very cheap (large mallocs are not nearly as expensive as large
8080
// callocs).
8181
let mut buf = vec::with_capacity(cap);
82-
unsafe { vec::raw::set_len(&mut buf, cap); }
82+
unsafe { buf.set_len(cap); }
8383
BufferedReader {
8484
inner: inner,
8585
buf: buf,
@@ -154,7 +154,7 @@ impl<W: Writer> BufferedWriter<W> {
154154
pub fn with_capacity(cap: uint, inner: W) -> BufferedWriter<W> {
155155
// See comments in BufferedReader for why this uses unsafe code.
156156
let mut buf = vec::with_capacity(cap);
157-
unsafe { vec::raw::set_len(&mut buf, cap); }
157+
unsafe { buf.set_len(cap); }
158158
BufferedWriter {
159159
inner: inner,
160160
buf: buf,

src/libstd/io/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ pub trait Reader {
523523
let mut total_read = 0;
524524

525525
buf.reserve_additional(len);
526-
vec::raw::set_len(buf, start_len + len);
526+
buf.set_len(start_len + len);
527527

528528
(|| {
529529
while total_read < len {
@@ -539,7 +539,7 @@ pub trait Reader {
539539
}
540540
}
541541
}
542-
}).finally(|| vec::raw::set_len(buf, start_len + total_read))
542+
}).finally(|| buf.set_len(start_len + total_read))
543543
}
544544
}
545545

src/libstd/io/native/file.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ pub fn readlink(p: &CString) -> IoResult<Path> {
706706
-1 => Err(super::last_error()),
707707
n => {
708708
assert!(n > 0);
709-
unsafe { vec::raw::set_len(&mut buf, n as uint); }
709+
unsafe { buf.set_len(n as uint); }
710710
Ok(Path::new(buf))
711711
}
712712
}

src/libstd/os.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ pub fn self_exe_path() -> Option<Path> {
369369
});
370370
if err != 0 { return None; }
371371
if sz == 0 { return None; }
372-
vec::raw::set_len(&mut v, sz as uint - 1); // chop off trailing NUL
372+
v.set_len(sz as uint - 1); // chop off trailing NUL
373373
Some(v)
374374
}
375375
}
@@ -398,7 +398,7 @@ pub fn self_exe_path() -> Option<Path> {
398398
_NSGetExecutablePath(buf as *mut i8, &mut sz)
399399
});
400400
if err != 0 { return None; }
401-
vec::raw::set_len(&mut v, sz as uint - 1); // chop off trailing NUL
401+
v.set_len(sz as uint - 1); // chop off trailing NUL
402402
Some(v)
403403
}
404404
}

src/libstd/rt/stack.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl StackSegment {
2424
unsafe {
2525
// Crate a block of uninitialized values
2626
let mut stack = vec::with_capacity(size);
27-
vec::raw::set_len(&mut stack, size);
27+
stack.set_len(size);
2828

2929
let mut stk = StackSegment {
3030
buf: stack,

src/libstd/str.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ pub mod raw {
993993
use cast;
994994
use libc;
995995
use ptr;
996-
use str::is_utf8;
996+
use str::{is_utf8, OwnedStr};
997997
use vec;
998998
use vec::MutableVector;
999999
use unstable::raw::Slice;
@@ -1002,7 +1002,7 @@ pub mod raw {
10021002
pub unsafe fn from_buf_len(buf: *u8, len: uint) -> ~str {
10031003
let mut v: ~[u8] = vec::with_capacity(len);
10041004
v.as_mut_buf(|vbuf, _len| ptr::copy_memory(vbuf, buf as *u8, len));
1005-
vec::raw::set_len(&mut v, len);
1005+
v.set_len(len);
10061006

10071007
assert!(is_utf8(v));
10081008
::cast::transmute(v)
@@ -1109,7 +1109,7 @@ pub mod raw {
11091109
let len = s.len();
11101110
assert!((len > 0u));
11111111
let b = s[len - 1u];
1112-
set_len(s, len - 1u);
1112+
s.set_len(len - 1);
11131113
return b;
11141114
}
11151115

@@ -1130,16 +1130,6 @@ pub mod raw {
11301130
cast::transmute(s)
11311131
}
11321132

1133-
/// Sets the length of a string
1134-
///
1135-
/// This will explicitly set the size of the string, without actually
1136-
/// modifying its buffers, so it is up to the caller to ensure that
1137-
/// the string is actually the specified size.
1138-
#[inline]
1139-
pub unsafe fn set_len(s: &mut ~str, new_len: uint) {
1140-
vec::raw::set_len(as_owned_vec(s), new_len)
1141-
}
1142-
11431133
/// Sets the length of a string
11441134
///
11451135
/// This will explicitly set the size of the string, without actually
@@ -1339,7 +1329,7 @@ impl Mutable for ~str {
13391329
#[inline]
13401330
fn clear(&mut self) {
13411331
unsafe {
1342-
raw::set_len(self, 0)
1332+
self.set_len(0)
13431333
}
13441334
}
13451335
}
@@ -2293,7 +2283,7 @@ impl<'a> StrSlice<'a> for &'a str {
22932283
let mut v = vec::with_capacity(len);
22942284

22952285
v.as_mut_buf(|dst, _| ptr::copy_memory(dst, src, len));
2296-
vec::raw::set_len(&mut v, len);
2286+
v.set_len(len);
22972287
::cast::transmute(v)
22982288
}
22992289
})
@@ -2598,6 +2588,13 @@ pub trait OwnedStr {
25982588
/// The caller must make sure any mutations to this buffer keep the string
25992589
/// valid UTF-8!
26002590
fn as_mut_buf<T>(&mut self, f: |*mut u8, uint| -> T) -> T;
2591+
2592+
/// Sets the length of a string
2593+
///
2594+
/// This will explicitly set the size of the string, without actually
2595+
/// modifying its buffers, so it is up to the caller to ensure that
2596+
/// the string is actually the specified size.
2597+
unsafe fn set_len(&mut self, new_len: uint);
26012598
}
26022599

26032600
impl OwnedStr for ~str {
@@ -2629,7 +2626,7 @@ impl OwnedStr for ~str {
26292626
c.encode_utf8(slc)
26302627
})
26312628
});
2632-
raw::set_len(self, cur_len + used);
2629+
self.set_len(cur_len + used);
26332630
}
26342631
}
26352632

@@ -2638,7 +2635,7 @@ impl OwnedStr for ~str {
26382635
let end = self.len();
26392636
assert!(end > 0u);
26402637
let CharRange {ch, next} = self.char_range_at_reverse(end);
2641-
unsafe { raw::set_len(self, next); }
2638+
unsafe { self.set_len(next); }
26422639
return ch;
26432640
}
26442641

@@ -2689,7 +2686,7 @@ impl OwnedStr for ~str {
26892686
fn truncate(&mut self, len: uint) {
26902687
assert!(len <= self.len());
26912688
assert!(self.is_char_boundary(len));
2692-
unsafe { raw::set_len(self, len); }
2689+
unsafe { self.set_len(len); }
26932690
}
26942691

26952692
#[inline]
@@ -2703,6 +2700,11 @@ impl OwnedStr for ~str {
27032700
raw::as_owned_vec(self).as_mut_buf(f)
27042701
}
27052702
}
2703+
2704+
#[inline]
2705+
unsafe fn set_len(&mut self, new_len: uint) {
2706+
raw::as_owned_vec(self).set_len(new_len)
2707+
}
27062708
}
27072709

27082710
impl Clone for ~str {

0 commit comments

Comments
 (0)