Skip to content

Make some slice coercions explicit - part 2 #18667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,11 @@ impl String {
/// // 𝄞music
/// let mut v = [0xD834, 0xDD1E, 0x006d, 0x0075,
/// 0x0073, 0x0069, 0x0063];
/// assert_eq!(String::from_utf16(v), Some("𝄞music".to_string()));
/// assert_eq!(String::from_utf16(&v), Some("𝄞music".to_string()));
///
/// // 𝄞mu<invalid>ic
/// v[4] = 0xD800;
/// assert_eq!(String::from_utf16(v), None);
/// assert_eq!(String::from_utf16(&v), None);
/// ```
#[unstable = "error value in return may change"]
pub fn from_utf16(v: &[u16]) -> Option<String> {
Expand All @@ -274,7 +274,7 @@ impl String {
/// 0x0073, 0xDD1E, 0x0069, 0x0063,
/// 0xD834];
///
/// assert_eq!(String::from_utf16_lossy(v),
/// assert_eq!(String::from_utf16_lossy(&v),
/// "𝄞mus\uFFFDic\uFFFD".to_string());
/// ```
#[stable]
Expand All @@ -288,7 +288,7 @@ impl String {
///
/// ```rust
/// let chars = ['h', 'e', 'l', 'l', 'o'];
/// let s = String::from_chars(chars);
/// let s = String::from_chars(&chars);
/// assert_eq!(s.as_slice(), "hello");
/// ```
#[inline]
Expand Down Expand Up @@ -592,7 +592,7 @@ impl String {
assert!(self.as_slice().is_char_boundary(idx));
self.vec.reserve_additional(4);
let mut bits = [0, ..4];
let amt = ch.encode_utf8(bits).unwrap();
let amt = ch.encode_utf8(&mut bits).unwrap();

unsafe {
ptr::copy_memory(self.vec.as_mut_ptr().offset((idx + amt) as int),
Expand Down Expand Up @@ -981,30 +981,30 @@ mod tests {
fn test_utf16_invalid() {
// completely positive cases tested above.
// lead + eof
assert_eq!(String::from_utf16([0xD800]), None);
assert_eq!(String::from_utf16(&[0xD800]), None);
// lead + lead
assert_eq!(String::from_utf16([0xD800, 0xD800]), None);
assert_eq!(String::from_utf16(&[0xD800, 0xD800]), None);

// isolated trail
assert_eq!(String::from_utf16([0x0061, 0xDC00]), None);
assert_eq!(String::from_utf16(&[0x0061, 0xDC00]), None);

// general
assert_eq!(String::from_utf16([0xD800, 0xd801, 0xdc8b, 0xD800]), None);
assert_eq!(String::from_utf16(&[0xD800, 0xd801, 0xdc8b, 0xD800]), None);
}

#[test]
fn test_from_utf16_lossy() {
// completely positive cases tested above.
// lead + eof
assert_eq!(String::from_utf16_lossy([0xD800]), String::from_str("\uFFFD"));
assert_eq!(String::from_utf16_lossy(&[0xD800]), String::from_str("\uFFFD"));
// lead + lead
assert_eq!(String::from_utf16_lossy([0xD800, 0xD800]), String::from_str("\uFFFD\uFFFD"));
assert_eq!(String::from_utf16_lossy(&[0xD800, 0xD800]), String::from_str("\uFFFD\uFFFD"));

// isolated trail
assert_eq!(String::from_utf16_lossy([0x0061, 0xDC00]), String::from_str("a\uFFFD"));
assert_eq!(String::from_utf16_lossy(&[0x0061, 0xDC00]), String::from_str("a\uFFFD"));

// general
assert_eq!(String::from_utf16_lossy([0xD800, 0xd801, 0xdc8b, 0xD800]),
assert_eq!(String::from_utf16_lossy(&[0xD800, 0xd801, 0xdc8b, 0xD800]),
String::from_str("\uFFFD𐒋\uFFFD"));
}

Expand All @@ -1031,7 +1031,7 @@ mod tests {
let mut s = String::from_str("ABC");
unsafe {
let mv = s.as_mut_vec();
mv.push_all([b'D']);
mv.push_all(&[b'D']);
}
assert_eq!(s.as_slice(), "ABCD");
}
Expand Down
58 changes: 29 additions & 29 deletions src/libcollections/tree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,14 +840,14 @@ mod test {
check(a, b, expected, |x, y, f| x.intersection(y).all(f))
}

check_intersection([], [], []);
check_intersection([1, 2, 3], [], []);
check_intersection([], [1, 2, 3], []);
check_intersection([2], [1, 2, 3], [2]);
check_intersection([1, 2, 3], [2], [2]);
check_intersection([11, 1, 3, 77, 103, 5, -5],
[2, 11, 77, -9, -42, 5, 3],
[3, 5, 11, 77]);
check_intersection(&[], &[], &[]);
check_intersection(&[1, 2, 3], &[], &[]);
check_intersection(&[], &[1, 2, 3], &[]);
check_intersection(&[2], &[1, 2, 3], &[2]);
check_intersection(&[1, 2, 3], &[2], &[2]);
check_intersection(&[11, 1, 3, 77, 103, 5, -5],
&[2, 11, 77, -9, -42, 5, 3],
&[3, 5, 11, 77]);
}

#[test]
Expand All @@ -856,15 +856,15 @@ mod test {
check(a, b, expected, |x, y, f| x.difference(y).all(f))
}

check_difference([], [], []);
check_difference([1, 12], [], [1, 12]);
check_difference([], [1, 2, 3, 9], []);
check_difference([1, 3, 5, 9, 11],
[3, 9],
[1, 5, 11]);
check_difference([-5, 11, 22, 33, 40, 42],
[-12, -5, 14, 23, 34, 38, 39, 50],
[11, 22, 33, 40, 42]);
check_difference(&[], &[], &[]);
check_difference(&[1, 12], &[], &[1, 12]);
check_difference(&[], &[1, 2, 3, 9], &[]);
check_difference(&[1, 3, 5, 9, 11],
&[3, 9],
&[1, 5, 11]);
check_difference(&[-5, 11, 22, 33, 40, 42],
&[-12, -5, 14, 23, 34, 38, 39, 50],
&[11, 22, 33, 40, 42]);
}

#[test]
Expand All @@ -874,12 +874,12 @@ mod test {
check(a, b, expected, |x, y, f| x.symmetric_difference(y).all(f))
}

check_symmetric_difference([], [], []);
check_symmetric_difference([1, 2, 3], [2], [1, 3]);
check_symmetric_difference([2], [1, 2, 3], [1, 3]);
check_symmetric_difference([1, 3, 5, 9, 11],
[-2, 3, 9, 14, 22],
[-2, 1, 5, 11, 14, 22]);
check_symmetric_difference(&[], &[], &[]);
check_symmetric_difference(&[1, 2, 3], &[2], &[1, 3]);
check_symmetric_difference(&[2], &[1, 2, 3], &[1, 3]);
check_symmetric_difference(&[1, 3, 5, 9, 11],
&[-2, 3, 9, 14, 22],
&[-2, 1, 5, 11, 14, 22]);
}

#[test]
Expand All @@ -889,12 +889,12 @@ mod test {
check(a, b, expected, |x, y, f| x.union(y).all(f))
}

check_union([], [], []);
check_union([1, 2, 3], [2], [1, 2, 3]);
check_union([2], [1, 2, 3], [1, 2, 3]);
check_union([1, 3, 5, 9, 11, 16, 19, 24],
[-2, 1, 5, 9, 13, 19],
[-2, 1, 3, 5, 9, 11, 13, 16, 19, 24]);
check_union(&[], &[], &[]);
check_union(&[1, 2, 3], &[2], &[1, 2, 3]);
check_union(&[2], &[1, 2, 3], &[1, 2, 3]);
check_union(&[1, 3, 5, 9, 11, 16, 19, 24],
&[-2, 1, 5, 9, 13, 19],
&[-2, 1, 3, 5, 9, 11, 13, 16, 19, 24]);
}

#[test]
Expand Down
16 changes: 8 additions & 8 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use slice::{CloneableVector};
/// vec[0] = 7i;
/// assert_eq!(vec[0], 7);
///
/// vec.push_all([1, 2, 3]);
/// vec.push_all(&[1, 2, 3]);
///
/// for x in vec.iter() {
/// println!("{}", x);
Expand Down Expand Up @@ -306,7 +306,7 @@ impl<T: Clone> Vec<T> {
///
/// ```
/// let mut vec = vec![1i];
/// vec.push_all([2i, 3, 4]);
/// vec.push_all(&[2i, 3, 4]);
/// assert_eq!(vec, vec![1, 2, 3, 4]);
/// ```
#[inline]
Expand Down Expand Up @@ -643,7 +643,7 @@ impl<T> Vec<T> {
///
/// ```
/// let mut vec: Vec<int> = Vec::with_capacity(10);
/// vec.push_all([1, 2, 3]);
/// vec.push_all(&[1, 2, 3]);
/// assert_eq!(vec.capacity(), 10);
/// vec.shrink_to_fit();
/// assert!(vec.capacity() >= 3);
Expand Down Expand Up @@ -1680,7 +1680,7 @@ mod tests {
#[test]
fn test_as_vec() {
let xs = [1u8, 2u8, 3u8];
assert_eq!(as_vec(xs).as_slice(), xs.as_slice());
assert_eq!(as_vec(&xs).as_slice(), xs.as_slice());
}

#[test]
Expand Down Expand Up @@ -1770,27 +1770,27 @@ mod tests {
let mut values = vec![1u8,2,3,4,5];
{
let slice = values.slice_from_mut(2);
assert!(slice == [3, 4, 5]);
assert!(slice == &mut [3, 4, 5]);
for p in slice.iter_mut() {
*p += 2;
}
}

assert!(values.as_slice() == [1, 2, 5, 6, 7]);
assert!(values.as_slice() == &[1, 2, 5, 6, 7]);
}

#[test]
fn test_slice_to_mut() {
let mut values = vec![1u8,2,3,4,5];
{
let slice = values.slice_to_mut(2);
assert!(slice == [1, 2]);
assert!(slice == &mut [1, 2]);
for p in slice.iter_mut() {
*p += 1;
}
}

assert!(values.as_slice() == [2, 3, 3, 4, 5]);
assert!(values.as_slice() == &[2, 3, 3, 4, 5]);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/finally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<T> Finally<T> for fn() -> T {
*
* struct State<'a> { buffer: &'a mut [u8], len: uint }
* # let mut buf = [];
* let mut state = State { buffer: buf, len: 0 };
* let mut state = State { buffer: &mut buf, len: 0 };
* try_finally(
* &mut state, (),
* |state, ()| {
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
}
}

let mut filler = Filler { buf: buf, end: &mut end };
let mut filler = Filler { buf: &mut buf, end: &mut end };
match sign {
SignNeg => {
let _ = format_args!(|args| {
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl<'a> Formatter<'a> {
let write_prefix = |f: &mut Formatter| {
for c in sign.into_iter() {
let mut b = [0, ..4];
let n = c.encode_utf8(b).unwrap_or(0);
let n = c.encode_utf8(&mut b).unwrap_or(0);
try!(f.buf.write(b[..n]));
}
if prefixed { f.buf.write(prefix.as_bytes()) }
Expand Down Expand Up @@ -497,7 +497,7 @@ impl<'a> Formatter<'a> {
};

let mut fill = [0u8, ..4];
let len = self.fill.encode_utf8(fill).unwrap_or(0);
let len = self.fill.encode_utf8(&mut fill).unwrap_or(0);

for _ in range(0, pre_pad) {
try!(self.buf.write(fill[..len]));
Expand Down Expand Up @@ -586,7 +586,7 @@ impl Char for char {
use char::Char;

let mut utf8 = [0u8, ..4];
let amt = self.encode_utf8(utf8).unwrap_or(0);
let amt = self.encode_utf8(&mut utf8).unwrap_or(0);
let s: &str = unsafe { mem::transmute(utf8[..amt]) };
String::fmt(s, f)
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ extern "rust-intrinsic" {
/// use std::mem;
///
/// let v: &[u8] = unsafe { mem::transmute("L") };
/// assert!(v == [76u8]);
/// assert!(v == &[76u8]);
/// ```
pub fn transmute<T,U>(e: T) -> U;

Expand Down
4 changes: 2 additions & 2 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ impl<T> Option<T> {
/// let mut x = Some("Diamonds");
/// {
/// let v = x.as_mut_slice();
/// assert!(v == ["Diamonds"]);
/// assert!(v == &mut ["Diamonds"]);
/// v[0] = "Dirt";
/// assert!(v == ["Dirt"]);
/// assert!(v == &mut ["Dirt"]);
/// }
/// assert_eq!(x, Some("Dirt"));
/// ```
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,14 @@ impl<T, E> Result<T, E> {
/// let mut x: Result<&str, uint> = Ok("Gold");
/// {
/// let v = x.as_mut_slice();
/// assert!(v == ["Gold"]);
/// assert!(v == &mut ["Gold"]);
/// v[0] = "Silver";
/// assert!(v == ["Silver"]);
/// assert!(v == &mut ["Silver"]);
/// }
/// assert_eq!(x, Ok("Silver"));
///
/// let mut x: Result<&str, uint> = Err(45);
/// assert!(x.as_mut_slice() == []);
/// assert!(x.as_mut_slice() == &mut []);
/// ```
#[inline]
#[unstable = "waiting for mut conventions"]
Expand Down
12 changes: 6 additions & 6 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ pub trait MutableSlice<T> for Sized? {
/// ```rust
/// let mut v = ["a", "b", "c", "d"];
/// v.swap(1, 3);
/// assert!(v == ["a", "d", "c", "b"]);
/// assert!(v == &["a", "d", "c", "b"]);
/// ```
#[unstable = "waiting on final error conventions"]
fn swap(&mut self, a: uint, b: uint);
Expand Down Expand Up @@ -605,7 +605,7 @@ pub trait MutableSlice<T> for Sized? {
/// ```rust
/// let mut v = [1i, 2, 3];
/// v.reverse();
/// assert!(v == [3i, 2, 1]);
/// assert!(v == &[3i, 2, 1]);
/// ```
#[experimental = "may be moved to iterators instead"]
fn reverse(&mut self);
Expand Down Expand Up @@ -867,12 +867,12 @@ pub trait MutableCloneableSlice<T> for Sized? {
/// let mut dst = [0i, 0, 0];
/// let src = [1i, 2];
///
/// assert!(dst.clone_from_slice(src) == 2);
/// assert!(dst == [1, 2, 0]);
/// assert!(dst.clone_from_slice(&src) == 2);
/// assert!(dst == &[1, 2, 0]);
///
/// let src2 = [3i, 4, 5, 6];
/// assert!(dst.clone_from_slice(src2) == 3);
/// assert!(dst == [3i, 4, 5]);
/// assert!(dst.clone_from_slice(&src2) == 3);
/// assert!(dst == &[3i, 4, 5]);
/// ```
fn clone_from_slice(&mut self, &[T]) -> uint;
}
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ impl<'a> Iterator<Utf16Item> for Utf16Items<'a> {
/// 0x0073, 0xDD1E, 0x0069, 0x0063,
/// 0xD834];
///
/// assert_eq!(str::utf16_items(v).collect::<Vec<_>>(),
/// assert_eq!(str::utf16_items(&v).collect::<Vec<_>>(),
/// vec![ScalarValue('𝄞'),
/// ScalarValue('m'), ScalarValue('u'), ScalarValue('s'),
/// LoneSurrogate(0xDD1E),
Expand All @@ -996,12 +996,12 @@ pub fn utf16_items<'a>(v: &'a [u16]) -> Utf16Items<'a> {
/// // "abcd"
/// let mut v = ['a' as u16, 'b' as u16, 'c' as u16, 'd' as u16];
/// // no NULs so no change
/// assert_eq!(str::truncate_utf16_at_nul(v), v.as_slice());
/// assert_eq!(str::truncate_utf16_at_nul(&v), v.as_slice());
///
/// // "ab\0d"
/// v[2] = 0;
/// let b: &[_] = &['a' as u16, 'b' as u16];
/// assert_eq!(str::truncate_utf16_at_nul(v), b);
/// assert_eq!(str::truncate_utf16_at_nul(&v), b);
/// ```
pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] {
match v.iter().position(|c| *c == 0) {
Expand Down
Loading