pub unsafe trait Slice:
Send
+ Sync
+ 'static {
type Item: Send + Sync + 'static;
type Vec: BufferMut<Self>;
type TryFromSliceError;
// Required methods
fn to_slice(&self) -> &[Self::Item];
unsafe fn to_slice_mut(&mut self) -> &mut [Self::Item];
fn into_boxed_slice(self: Box<Self>) -> Box<[Self::Item]>;
fn into_vec(vec: Self::Vec) -> Vec<Self::Item>;
unsafe fn from_slice_unchecked(slice: &[Self::Item]) -> &Self;
unsafe fn from_slice_mut_unchecked(slice: &mut [Self::Item]) -> &mut Self;
unsafe fn from_boxed_slice_unchecked(boxed: Box<[Self::Item]>) -> Box<Self>;
unsafe fn from_vec_unchecked(vec: Vec<Self::Item>) -> Self::Vec;
fn try_from_slice(
slice: &[Self::Item],
) -> Result<&Self, Self::TryFromSliceError>;
fn try_from_slice_mut(
slice: &mut [Self::Item],
) -> Result<&mut Self, Self::TryFromSliceError>;
}Expand description
A slice, e.g. [T] or str.
§Safety
into_vecmust be pure, i.e.mem::forget(S::into_vec(ptr::read(vec_ptr)))should not invalidate memory behindvec_ptr.- If
try_from_slice_mutreturnsOkfor a slice, thentry_from_slicemust also returnOkfor that slice.
Required Associated Types§
Sourcetype Vec: BufferMut<Self>
type Vec: BufferMut<Self>
The associated vector to the slice type, e.g. Vec<T> for [T] or String for str.
Sourcetype TryFromSliceError
type TryFromSliceError
Error which can occur when attempting to convert an item slice to the given slice type.
Required Methods§
Sourceunsafe fn to_slice_mut(&mut self) -> &mut [Self::Item]
unsafe fn to_slice_mut(&mut self) -> &mut [Self::Item]
Converts a mutable slice to its underlying item slice.
§Safety
The item slice is never mutated, as it is only used for storage.
Sourcefn into_boxed_slice(self: Box<Self>) -> Box<[Self::Item]>
fn into_boxed_slice(self: Box<Self>) -> Box<[Self::Item]>
Converts a boxed slice to its underlying boxed item slice.
Sourcefn into_vec(vec: Self::Vec) -> Vec<Self::Item>
fn into_vec(vec: Self::Vec) -> Vec<Self::Item>
Converts a vector to its underlying item vector.
Sourceunsafe fn from_slice_unchecked(slice: &[Self::Item]) -> &Self
unsafe fn from_slice_unchecked(slice: &[Self::Item]) -> &Self
Converts back a slice from its underlying item slice.
§Safety
The item slice must be valid as if it has been obtained from Self::to_slice.
Sourceunsafe fn from_slice_mut_unchecked(slice: &mut [Self::Item]) -> &mut Self
unsafe fn from_slice_mut_unchecked(slice: &mut [Self::Item]) -> &mut Self
Converts back a mutable slice from its underlying item slice.
§Safety
The item slice must be valid as if it has been obtained from Self::to_slice_mut.
Sourceunsafe fn from_boxed_slice_unchecked(boxed: Box<[Self::Item]>) -> Box<Self>
unsafe fn from_boxed_slice_unchecked(boxed: Box<[Self::Item]>) -> Box<Self>
Converts back a boxed slice from its underlying boxed item slice.
§Safety
The boxed item slice must be valid as if it has been obtained from
Self::into_boxed_slice.
Sourceunsafe fn from_vec_unchecked(vec: Vec<Self::Item>) -> Self::Vec
unsafe fn from_vec_unchecked(vec: Vec<Self::Item>) -> Self::Vec
Converts back a vector from its underlying item vector.
§Safety
The vector must be valid as if it has been obtained from Self::into_vec.
Sourcefn try_from_slice(
slice: &[Self::Item],
) -> Result<&Self, Self::TryFromSliceError>
fn try_from_slice( slice: &[Self::Item], ) -> Result<&Self, Self::TryFromSliceError>
Try converting an item slice to the given slice type.
Sourcefn try_from_slice_mut(
slice: &mut [Self::Item],
) -> Result<&mut Self, Self::TryFromSliceError>
fn try_from_slice_mut( slice: &mut [Self::Item], ) -> Result<&mut Self, Self::TryFromSliceError>
Tries converting a mutable item slice to the given slice type.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl Slice for str
impl Slice for str
type Item = u8
type Vec = String
type TryFromSliceError = Utf8Error
fn to_slice(&self) -> &[Self::Item]
unsafe fn to_slice_mut(&mut self) -> &mut [Self::Item]
fn into_boxed_slice(self: Box<Self>) -> Box<[Self::Item]>
fn into_vec(vec: Self::Vec) -> Vec<Self::Item>
unsafe fn from_slice_unchecked(slice: &[Self::Item]) -> &Self
unsafe fn from_slice_mut_unchecked(slice: &mut [Self::Item]) -> &mut Self
unsafe fn from_boxed_slice_unchecked(boxed: Box<[Self::Item]>) -> Box<Self>
unsafe fn from_vec_unchecked(vec: Vec<Self::Item>) -> Self::Vec
fn try_from_slice( slice: &[Self::Item], ) -> Result<&Self, Self::TryFromSliceError>
fn try_from_slice_mut( slice: &mut [Self::Item], ) -> Result<&mut Self, Self::TryFromSliceError>
Source§impl Slice for BStr
Available on crate feature bstr only.
impl Slice for BStr
bstr only.