Struct std::raw::Slice
[−]
[src]
pub struct Slice<T> { pub data: *const T, pub len: usize, }
The representation of a slice like &[T]
`&[T]`.
This struct is guaranteed to have the layout of types like &[T]
`&[T],
`,
&str
`&str, and
`, and Box<[T]>
`Box<[T]>, but is not the type of such slices (e.g. the fields are not directly accessible on a
`, but is not the type of such slices
(e.g. the fields are not directly accessible on a &[T]
`&[T]) nor does it control that layout (changing the definition will not change the layout of a
`) nor does
it control that layout (changing the definition will not change
the layout of a &[T]
`&[T]`). It is only designed to be used by unsafe
code that needs to manipulate the low-level details.
However, it is not recommended to use this type for such code, since there are alternatives which may be safer:
- Creating a slice from a data pointer and length can be done with
std::slice::from_raw_parts
`std::slice::from_raw_partsor
` orstd::slice::from_raw_parts_mut
`std::slice::from_raw_parts_mutinstead of
` instead ofstd::mem::transmute
`std::mem::transmuteing a value of type
`ing a value of typeSlice
`Slice`. - Extracting the data pointer and length from a slice can be
performed with the
as_ptr
`as_ptr(or
` (oras_mut_ptr
`as_mut_ptr) and
`) andlen
`len` methods.
If one does decide to convert a slice value to a Slice
`Slice, the
`, the
Repr
`Reprtrait in this module provides a method for a safe conversion from
` trait in this module provides a method for a safe
conversion from &[T]
`&[T](and
` (and &str
`&str) to a
`) to a Slice
`Slice, more type-safe than a call to
`, more type-safe
than a call to transmute
`transmute`.
Examples
#![feature(core)] fn main() { use std::raw::{self, Repr}; let slice: &[u16] = &[1, 2, 3, 4]; let repr: raw::Slice<u16> = slice.repr(); println!("data pointer = {:?}, length = {}", repr.data, repr.len); }use std::raw::{self, Repr}; let slice: &[u16] = &[1, 2, 3, 4]; let repr: raw::Slice<u16> = slice.repr(); println!("data pointer = {:?}, length = {}", repr.data, repr.len);
Fields
data | Unstable |
len | Unstable |