Skip to content

Commit 766a4e1

Browse files
committed
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.
2 parents f7453f9 + 85080fa commit 766a4e1

File tree

11 files changed

+72
-80
lines changed

11 files changed

+72
-80
lines changed

src/libcollections/btree/node.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,8 @@ impl<K, V> Node<K, V> {
348348
#[inline]
349349
pub fn as_slices<'a>(&'a self) -> (&'a [K], &'a [V]) {
350350
unsafe {(
351-
mem::transmute(raw::Slice {
352-
data: *self.keys as *const K,
353-
len: self.len()
354-
}),
355-
mem::transmute(raw::Slice {
356-
data: *self.vals as *const V,
357-
len: self.len()
358-
})
351+
slice::from_raw_parts(*self.keys, self.len()),
352+
slice::from_raw_parts(*self.vals, self.len()),
359353
)}
360354
}
361355

src/libcollections/string.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use core::iter::{IntoIterator, FromIterator};
2424
use core::mem;
2525
use core::ops::{self, Deref, Add, Index};
2626
use core::ptr;
27-
use core::raw::Slice as RawSlice;
27+
use core::slice;
2828
use unicode::str as unicode_str;
2929
use unicode::str::Utf16Item;
3030

@@ -468,11 +468,11 @@ impl String {
468468
unsafe {
469469
// Attempt to not use an intermediate buffer by just pushing bytes
470470
// directly onto this string.
471-
let slice = RawSlice {
472-
data: self.vec.as_ptr().offset(cur_len as isize),
473-
len: 4,
474-
};
475-
let used = ch.encode_utf8(mem::transmute(slice)).unwrap_or(0);
471+
let slice = slice::from_raw_parts_mut (
472+
self.vec.as_mut_ptr().offset(cur_len as isize),
473+
4
474+
);
475+
let used = ch.encode_utf8(slice).unwrap_or(0);
476476
self.vec.set_len(cur_len + used);
477477
}
478478
}

src/libcollections/vec.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ use core::ops::{Index, IndexMut, Deref, Add};
6464
use core::ops;
6565
use core::ptr;
6666
use core::ptr::Unique;
67-
use core::raw::Slice as RawSlice;
6867
use core::slice;
6968
use core::usize;
7069

@@ -435,10 +434,7 @@ impl<T> Vec<T> {
435434
unsafe {
436435
let ptr = *self.ptr;
437436
assume(!ptr.is_null());
438-
mem::transmute(RawSlice {
439-
data: ptr,
440-
len: self.len,
441-
})
437+
slice::from_raw_parts_mut(ptr, self.len)
442438
}
443439
}
444440

@@ -1560,10 +1556,7 @@ impl<T> AsSlice<T> for Vec<T> {
15601556
unsafe {
15611557
let p = *self.ptr;
15621558
assume(p != 0 as *mut T);
1563-
mem::transmute(RawSlice {
1564-
data: p,
1565-
len: self.len
1566-
})
1559+
slice::from_raw_parts(p, self.len)
15671560
}
15681561
}
15691562
}

src/libcollections/vec_deque.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use core::num::{Int, UnsignedInt};
2929
use core::num::wrapping::WrappingOps;
3030
use core::ops::{Index, IndexMut};
3131
use core::ptr::{self, Unique};
32-
use core::raw::Slice as RawSlice;
32+
use core::slice;
3333

3434
use core::hash::{Hash, Hasher};
3535
use core::cmp;
@@ -91,13 +91,13 @@ impl<T> VecDeque<T> {
9191
/// Turn ptr into a slice
9292
#[inline]
9393
unsafe fn buffer_as_slice(&self) -> &[T] {
94-
mem::transmute(RawSlice { data: *self.ptr as *const T, len: self.cap })
94+
slice::from_raw_parts(*self.ptr, self.cap)
9595
}
9696

9797
/// Turn ptr into a mut slice
9898
#[inline]
9999
unsafe fn buffer_as_mut_slice(&mut self) -> &mut [T] {
100-
mem::transmute(RawSlice { data: *self.ptr as *const T, len: self.cap })
100+
slice::from_raw_parts_mut(*self.ptr, self.cap)
101101
}
102102

103103
/// Moves an element out of the buffer

src/libcore/slice.rs

+27-14
Original file line numberDiff line numberDiff line change
@@ -520,10 +520,10 @@ impl<T> ops::Index<ops::Range<usize>> for [T] {
520520
assert!(index.start <= index.end);
521521
assert!(index.end <= self.len());
522522
unsafe {
523-
transmute(RawSlice {
524-
data: self.as_ptr().offset(index.start as isize),
525-
len: index.end - index.start
526-
})
523+
from_raw_parts (
524+
self.as_ptr().offset(index.start as isize),
525+
index.end - index.start
526+
)
527527
}
528528
}
529529
}
@@ -559,10 +559,10 @@ impl<T> ops::IndexMut<ops::Range<usize>> for [T] {
559559
assert!(index.start <= index.end);
560560
assert!(index.end <= self.len());
561561
unsafe {
562-
transmute(RawSlice {
563-
data: self.as_ptr().offset(index.start as isize),
564-
len: index.end - index.start
565-
})
562+
from_raw_parts_mut(
563+
self.as_mut_ptr().offset(index.start as isize),
564+
index.end - index.start
565+
)
566566
}
567567
}
568568
}
@@ -731,7 +731,21 @@ macro_rules! make_slice {
731731
diff / mem::size_of::<$t>()
732732
};
733733
unsafe {
734-
transmute::<_, $result>(RawSlice { data: $start, len: len })
734+
from_raw_parts($start, len)
735+
}
736+
}}
737+
}
738+
739+
macro_rules! make_mut_slice {
740+
($t: ty => $result: ty: $start: expr, $end: expr) => {{
741+
let diff = $end as usize - $start as usize;
742+
let len = if mem::size_of::<T>() == 0 {
743+
diff
744+
} else {
745+
diff / mem::size_of::<$t>()
746+
};
747+
unsafe {
748+
from_raw_parts_mut($start, len)
735749
}
736750
}}
737751
}
@@ -898,7 +912,7 @@ impl<'a, T> ops::IndexMut<ops::RangeFrom<usize>> for IterMut<'a, T> {
898912
impl<'a, T> ops::IndexMut<RangeFull> for IterMut<'a, T> {
899913
#[inline]
900914
fn index_mut(&mut self, _index: &RangeFull) -> &mut [T] {
901-
make_slice!(T => &mut [T]: self.ptr, self.end)
915+
make_mut_slice!(T => &mut [T]: self.ptr, self.end)
902916
}
903917
}
904918

@@ -912,7 +926,7 @@ impl<'a, T> IterMut<'a, T> {
912926
/// restricted lifetimes that do not consume the iterator.
913927
#[unstable(feature = "core")]
914928
pub fn into_slice(self) -> &'a mut [T] {
915-
make_slice!(T => &'a mut [T]: self.ptr, self.end)
929+
make_mut_slice!(T => &'a mut [T]: self.ptr, self.end)
916930
}
917931
}
918932

@@ -1404,16 +1418,15 @@ impl<'a, T> ExactSizeIterator for ChunksMut<'a, T> {}
14041418
#[unstable(feature = "core")]
14051419
pub fn ref_slice<'a, A>(s: &'a A) -> &'a [A] {
14061420
unsafe {
1407-
transmute(RawSlice { data: s, len: 1 })
1421+
from_raw_parts(s, 1)
14081422
}
14091423
}
14101424

14111425
/// Converts a pointer to A into a slice of length 1 (without copying).
14121426
#[unstable(feature = "core")]
14131427
pub fn mut_ref_slice<'a, A>(s: &'a mut A) -> &'a mut [A] {
14141428
unsafe {
1415-
let ptr: *const A = transmute(s);
1416-
transmute(RawSlice { data: ptr, len: 1 })
1429+
from_raw_parts_mut(s, 1)
14171430
}
14181431
}
14191432

src/librustc_llvm/archive_ro.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use libc;
1414
use ArchiveRef;
1515

1616
use std::ffi::CString;
17-
use std::mem;
18-
use std::raw;
17+
use std::slice;
1918
use std::path::Path;
2019

2120
pub struct ArchiveRO {
@@ -62,10 +61,7 @@ impl ArchiveRO {
6261
if ptr.is_null() {
6362
None
6463
} else {
65-
Some(mem::transmute(raw::Slice {
66-
data: ptr,
67-
len: size as uint,
68-
}))
64+
Some(slice::from_raw_parts(ptr as *const u8, size as uint))
6965
}
7066
}
7167
}

src/librustc_llvm/lib.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
#![feature(box_syntax)]
2828
#![feature(collections)]
29-
#![feature(core)]
3029
#![feature(int_uint)]
3130
#![feature(libc)]
3231
#![feature(link_args)]
@@ -58,7 +57,7 @@ pub use self::Linkage::*;
5857

5958
use std::ffi::CString;
6059
use std::cell::RefCell;
61-
use std::{raw, mem};
60+
use std::{slice, mem};
6261
use libc::{c_uint, c_ushort, uint64_t, c_int, size_t, c_char};
6362
use libc::{c_longlong, c_ulonglong, c_void};
6463
use debuginfo::{DIBuilderRef, DIDescriptor,
@@ -2226,10 +2225,7 @@ type RustStringRepr = *mut RefCell<Vec<u8>>;
22262225
pub unsafe extern "C" fn rust_llvm_string_write_impl(sr: RustStringRef,
22272226
ptr: *const c_char,
22282227
size: size_t) {
2229-
let slice: &[u8] = mem::transmute(raw::Slice {
2230-
data: ptr as *const u8,
2231-
len: size as uint,
2232-
});
2228+
let slice = slice::from_raw_parts(ptr as *const u8, size as uint);
22332229

22342230
let sr: RustStringRepr = mem::transmute(sr);
22352231
(*sr).borrow_mut().push_all(slice);

src/libstd/old_io/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -931,15 +931,15 @@ impl<'a> Reader for &'a mut (Reader+'a) {
931931
// Private function here because we aren't sure if we want to expose this as
932932
// API yet. If so, it should be a method on Vec.
933933
unsafe fn slice_vec_capacity<'a, T>(v: &'a mut Vec<T>, start: uint, end: uint) -> &'a mut [T] {
934-
use raw::Slice;
934+
use slice;
935935
use ptr::PtrExt;
936936

937937
assert!(start <= end);
938938
assert!(end <= v.capacity());
939-
transmute(Slice {
940-
data: v.as_ptr().offset(start as int),
941-
len: end - start
942-
})
939+
slice::from_raw_parts_mut(
940+
v.as_mut_ptr().offset(start as int),
941+
end - start
942+
)
943943
}
944944

945945
/// A `RefReader` is a struct implementing `Reader` which contains a reference

src/libstd/sys/common/wtf8.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use core::prelude::*;
2929

3030
use core::char::{encode_utf8_raw, encode_utf16_raw};
3131
use core::str::{char_range_at_raw, next_code_point};
32-
use core::raw::Slice as RawSlice;
3332

3433
use ascii::*;
3534
use borrow::Cow;
@@ -214,10 +213,10 @@ impl Wtf8Buf {
214213
unsafe {
215214
// Attempt to not use an intermediate buffer by just pushing bytes
216215
// directly onto this string.
217-
let slice = RawSlice {
218-
data: self.bytes.as_ptr().offset(cur_len as int),
219-
len: 4,
220-
};
216+
let slice = slice::from_raw_parts_mut(
217+
self.bytes.as_mut_ptr().offset(cur_len as int),
218+
4
219+
);
221220
let used = encode_utf8_raw(code_point.value, mem::transmute(slice))
222221
.unwrap_or(0);
223222
self.bytes.set_len(cur_len + used);
@@ -725,10 +724,11 @@ pub fn is_code_point_boundary(slice: &Wtf8, index: uint) -> bool {
725724
/// Copied from core::str::raw::slice_unchecked
726725
#[inline]
727726
pub unsafe fn slice_unchecked(s: &Wtf8, begin: uint, end: uint) -> &Wtf8 {
728-
mem::transmute(RawSlice {
729-
data: s.bytes.as_ptr().offset(begin as int),
730-
len: end - begin,
731-
})
727+
// memory layout of an &[u8] and &Wtf8 are the same
728+
mem::transmute(slice::from_raw_parts(
729+
s.bytes.as_ptr().offset(begin as int),
730+
end - begin
731+
))
732732
}
733733

734734
/// Copied from core::str::raw::slice_error_fail

src/test/run-pass/method-mut-self-modifies-mut-slice-lvalue.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// type is `&mut [u8]`, passes in a pointer to the lvalue and not a
1313
// temporary. Issue #19147.
1414

15-
use std::raw;
1615
use std::mem;
1716
use std::slice;
1817
use std::old_io::IoResult;
@@ -27,10 +26,10 @@ impl<'a> MyWriter for &'a mut [u8] {
2726

2827
let write_len = buf.len();
2928
unsafe {
30-
*self = mem::transmute(raw::Slice {
31-
data: self.as_ptr().offset(write_len as int),
32-
len: self.len() - write_len,
33-
});
29+
*self = slice::from_raw_parts_mut(
30+
self.as_mut_ptr().offset(write_len as isize),
31+
self.len() - write_len
32+
);
3433
}
3534

3635
Ok(())

src/test/run-pass/unsized3.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,32 @@
1515

1616
use std::mem;
1717
use std::raw;
18+
use std::slice;
1819

1920
struct Foo<T> {
2021
f: [T],
2122
}
2223

2324
struct Bar {
24-
f1: uint,
25-
f2: [uint],
25+
f1: usize,
26+
f2: [usize],
2627
}
2728

2829
struct Baz {
29-
f1: uint,
30+
f1: usize,
3031
f2: str,
3132
}
3233

3334
trait Tr {
34-
fn foo(&self) -> uint;
35+
fn foo(&self) -> usize;
3536
}
3637

3738
struct St {
38-
f: uint
39+
f: usize
3940
}
4041

4142
impl Tr for St {
42-
fn foo(&self) -> uint {
43+
fn foo(&self) -> usize {
4344
self.f
4445
}
4546
}
@@ -67,18 +68,18 @@ pub fn main() {
6768
}
6869

6970
let data: Box<Foo_<i32>> = box Foo_{f: [1, 2, 3] };
70-
let x: &Foo<i32> = mem::transmute(raw::Slice { len: 3, data: &*data });
71+
let x: &Foo<i32> = mem::transmute(slice::from_raw_parts(&*data, 3));
7172
assert!(x.f.len() == 3);
7273
assert!(x.f[0] == 1);
7374

7475
struct Baz_ {
75-
f1: uint,
76+
f1: usize,
7677
f2: [u8; 5],
7778
}
7879

7980
let data: Box<_> = box Baz_ {
8081
f1: 42, f2: ['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8, 'e' as u8] };
81-
let x: &Baz = mem::transmute( raw::Slice { len: 5, data: &*data } );
82+
let x: &Baz = mem::transmute(slice::from_raw_parts(&*data, 5));
8283
assert!(x.f1 == 42);
8384
let chs: Vec<char> = x.f2.chars().collect();
8485
assert!(chs.len() == 5);

0 commit comments

Comments
 (0)