Auto merge of #23333 - oli-obk:slice_from_raw_parts, r=alexcrichton
at least that's what the docs say: http://doc.rust-lang.org/std/slice/fn.from_raw_parts.html A few situations got prettier. In some situations the mutability of the resulting and source pointers differed (and was cast away by transmute), the mutability matches now.
This commit is contained in:
commit
766a4e1acc
11 changed files with 72 additions and 80 deletions
|
|
@ -12,7 +12,6 @@
|
|||
// type is `&mut [u8]`, passes in a pointer to the lvalue and not a
|
||||
// temporary. Issue #19147.
|
||||
|
||||
use std::raw;
|
||||
use std::mem;
|
||||
use std::slice;
|
||||
use std::old_io::IoResult;
|
||||
|
|
@ -27,10 +26,10 @@ impl<'a> MyWriter for &'a mut [u8] {
|
|||
|
||||
let write_len = buf.len();
|
||||
unsafe {
|
||||
*self = mem::transmute(raw::Slice {
|
||||
data: self.as_ptr().offset(write_len as int),
|
||||
len: self.len() - write_len,
|
||||
});
|
||||
*self = slice::from_raw_parts_mut(
|
||||
self.as_mut_ptr().offset(write_len as isize),
|
||||
self.len() - write_len
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -15,31 +15,32 @@
|
|||
|
||||
use std::mem;
|
||||
use std::raw;
|
||||
use std::slice;
|
||||
|
||||
struct Foo<T> {
|
||||
f: [T],
|
||||
}
|
||||
|
||||
struct Bar {
|
||||
f1: uint,
|
||||
f2: [uint],
|
||||
f1: usize,
|
||||
f2: [usize],
|
||||
}
|
||||
|
||||
struct Baz {
|
||||
f1: uint,
|
||||
f1: usize,
|
||||
f2: str,
|
||||
}
|
||||
|
||||
trait Tr {
|
||||
fn foo(&self) -> uint;
|
||||
fn foo(&self) -> usize;
|
||||
}
|
||||
|
||||
struct St {
|
||||
f: uint
|
||||
f: usize
|
||||
}
|
||||
|
||||
impl Tr for St {
|
||||
fn foo(&self) -> uint {
|
||||
fn foo(&self) -> usize {
|
||||
self.f
|
||||
}
|
||||
}
|
||||
|
|
@ -67,18 +68,18 @@ pub fn main() {
|
|||
}
|
||||
|
||||
let data: Box<Foo_<i32>> = box Foo_{f: [1, 2, 3] };
|
||||
let x: &Foo<i32> = mem::transmute(raw::Slice { len: 3, data: &*data });
|
||||
let x: &Foo<i32> = mem::transmute(slice::from_raw_parts(&*data, 3));
|
||||
assert!(x.f.len() == 3);
|
||||
assert!(x.f[0] == 1);
|
||||
|
||||
struct Baz_ {
|
||||
f1: uint,
|
||||
f1: usize,
|
||||
f2: [u8; 5],
|
||||
}
|
||||
|
||||
let data: Box<_> = box Baz_ {
|
||||
f1: 42, f2: ['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8, 'e' as u8] };
|
||||
let x: &Baz = mem::transmute( raw::Slice { len: 5, data: &*data } );
|
||||
let x: &Baz = mem::transmute(slice::from_raw_parts(&*data, 5));
|
||||
assert!(x.f1 == 42);
|
||||
let chs: Vec<char> = x.f2.chars().collect();
|
||||
assert!(chs.len() == 5);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue