pub struct DynSlice<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> { /* private fields */ }
Expand description
&dyn [Trait]
A type erased slice of elements that implement a trait.
§Example
use dyn_slice::standard::debug;
let slice = debug::new(&[1, 2, 3, 4, 5]);
println!("{slice:?}"); // [1, 2, 3, 4, 5]
Implementations§
source§impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> DynSlice<'a, Dyn>
impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> DynSlice<'a, Dyn>
sourcepub const unsafe fn with_vtable_ptr<DynSliceFromType>(
value: &'a [DynSliceFromType],
vtable_ptr: *const ()
) -> Self
pub const unsafe fn with_vtable_ptr<DynSliceFromType>( value: &'a [DynSliceFromType], vtable_ptr: *const () ) -> Self
Construct a dyn slice given a slice and a vtable pointer.
§Safety
Caller must ensure that vtable_ptr
is a valid instance of DynMetadata
for DynSliceFromType
and Dyn
transmuted, or optionally, a null pointer if value.len() == 0
.
sourcepub const unsafe fn with_metadata<DynSliceFromType>(
value: &'a [DynSliceFromType],
metadata: DynMetadata<Dyn>
) -> Self
pub const unsafe fn with_metadata<DynSliceFromType>( value: &'a [DynSliceFromType], metadata: DynMetadata<Dyn> ) -> Self
Construct a dyn slice given a slice and a DynMetadata
instance.
§Safety
Caller must ensure that metadata
is a valid instance of DynMetadata
for DynSliceFromType
and Dyn
.
sourcepub const unsafe fn from_parts(
vtable_ptr: *const (),
len: usize,
data: *const ()
) -> Self
pub const unsafe fn from_parts( vtable_ptr: *const (), len: usize, data: *const () ) -> Self
Construct a dyn slice from raw parts.
§Safety
Caller must ensure that:
vtable_ptr
is a valid instance ofDynMetadata
transmuted, or optionally, a null pointer iflen == 0
,len
<= the length of the slice in memory from thedata
pointer,data
is a valid pointer to the slice,- the underlying slice is the same layout as
[T]
sourcepub unsafe fn from_parts_with_metadata(
metadata: DynMetadata<Dyn>,
len: usize,
data: *const ()
) -> Self
pub unsafe fn from_parts_with_metadata( metadata: DynMetadata<Dyn>, len: usize, data: *const () ) -> Self
Construct a dyn slice from raw parts with a DynMetadata
instance rather than a vtable pointer.
§Safety
Caller must ensure that:
metadata
is a valid instance ofDynMetadata
,len
<= the length of the slice in memory from thedata
pointer,data
is a valid pointer to the slice,- the underlying slice is the same layout as
[T]
sourcepub const fn vtable_ptr(&self) -> *const ()
pub const fn vtable_ptr(&self) -> *const ()
Get the vtable pointer, which may be null if the slice is empty.
sourcepub fn metadata(&self) -> Option<DynMetadata<Dyn>>
pub fn metadata(&self) -> Option<DynMetadata<Dyn>>
Get the metadata component of the element’s pointers, or possibly None
if the slice is empty.
sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the number of elements in the slice.
§Example
use dyn_slice::standard::debug;
let slice = debug::new(&[1, 2, 3, 4, 5]);
assert_eq!(slice.len(), 5);
sourcepub const fn as_ptr(&self) -> *const ()
pub const fn as_ptr(&self) -> *const ()
Returns a pointer to the underlying slice, which may be null if the slice is empty.
sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true
if the slice has a length of 0.
§Example
use dyn_slice::standard::debug;
let slice = debug::new(&[1, 2, 3, 4, 5]);
assert!(!slice.is_empty());
let empty_slice = debug::new::<u8>(&[]);
assert!(empty_slice.is_empty());
sourcepub unsafe fn first_unchecked(&self) -> &Dyn
pub unsafe fn first_unchecked(&self) -> &Dyn
Returns a reference to the first element, without doing bounds checking.
§Safety
The caller must ensure that !self.is_empty()
Calling this on an empty DynSlice
will result in a segfault!
sourcepub fn first(&self) -> Option<&Dyn>
pub fn first(&self) -> Option<&Dyn>
Returns a reference to the first element of the slice, or None
if it is empty.
§Example
use dyn_slice::standard::debug;
let slice = debug::new(&[1, 2, 3, 4, 5]);
println!("{:?}", slice.first()); // Some(1)
let empty_slice = debug::new::<u8>(&[]);
println!("{:?}", empty_slice.first()); // None
sourcepub fn last(&self) -> Option<&Dyn>
pub fn last(&self) -> Option<&Dyn>
Returns a reference to the last element of the slice, or None
if it is empty.
§Example
use dyn_slice::standard::debug;
let slice = debug::new(&[1, 2, 3, 4, 5]);
println!("{:?}", slice.last()); // Some(5)
let empty_slice = debug::new::<u8>(&[]);
println!("{:?}", empty_slice.last()); // None
sourcepub fn get(&self, index: usize) -> Option<&Dyn>
pub fn get(&self, index: usize) -> Option<&Dyn>
Returns a reference to the element at the given index
or None
if the index
is out of bounds.
§Example
use dyn_slice::standard::debug;
let slice = debug::new(&[1, 2, 3, 4, 5]);
println!("{:?}", slice.get(2)); // Some(3)
println!("{:?}", slice.get(5)); // None
sourcepub unsafe fn get_unchecked(&self, index: usize) -> &Dyn
pub unsafe fn get_unchecked(&self, index: usize) -> &Dyn
Returns a reference to the element at the given index
, without doing bounds checking.
§Safety
The caller must ensure that index < self.len()
Calling this on an empty dyn Slice will result in a segfault!
sourcepub unsafe fn slice_unchecked(
&self,
start: usize,
len: usize
) -> DynSlice<'_, Dyn>
pub unsafe fn slice_unchecked( &self, start: usize, len: usize ) -> DynSlice<'_, Dyn>
Get a sub-slice from the start
index with the len
, without doing bounds checking.
§Safety
Caller must ensure that:
start < self.len()
len <= self.len() - start
sourcepub fn slice<R: RangeBounds<usize>>(
&self,
range: R
) -> Option<DynSlice<'_, Dyn>>
pub fn slice<R: RangeBounds<usize>>( &self, range: R ) -> Option<DynSlice<'_, Dyn>>
Returns a sub-slice from the start
index with the len
or None
if the slice is out of bounds.
§Example
use dyn_slice::standard::debug;
let slice = debug::new(&[1, 2, 3, 4, 5]);
println!("{slice:?}"); // [1, 2, 3, 4, 5]
println!("{:?}", slice.slice(1..4)); // Some([2, 3, 4])
println!("{:?}", slice.slice(2..)); // Some([3, 4, 5])
println!("{:?}", slice.slice(5..)); // Some([])
println!("{:?}", slice.slice(6..)); // None
sourcepub const unsafe fn downcast_unchecked<T>(&self) -> &[T]
pub const unsafe fn downcast_unchecked<T>(&self) -> &[T]
Returns the underlying slice as &[T]
.
§Safety
The caller must ensure that the underlying slice is of type [T]
.
sourcepub const fn iter(&self) -> Iter<'_, Dyn> ⓘ
pub const fn iter(&self) -> Iter<'_, Dyn> ⓘ
Returns an iterator over the slice.
§Example
use dyn_slice::standard::debug;
let slice = debug::new(&[1, 2, 3, 4, 5]);
let iter = slice.iter().map(|x| format!("{:?}!", x));
println!("{:?}", iter.collect::<Vec<String>>()); // ["1!", "2!", "3!", "4!", "5!"]
Trait Implementations§
source§impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> AsRef<DynSlice<'a, Dyn>> for DynSliceMut<'a, Dyn>
impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> AsRef<DynSlice<'a, Dyn>> for DynSliceMut<'a, Dyn>
source§impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + Debug + ?Sized> Debug for DynSlice<'a, Dyn>
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + Debug + ?Sized> Debug for DynSlice<'a, Dyn>
source§impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> Index<usize> for DynSlice<'a, Dyn>
impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> Index<usize> for DynSlice<'a, Dyn>
source§impl<'a, 'b, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for &'b DynSlice<'a, Dyn>
impl<'a, 'b, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for &'b DynSlice<'a, Dyn>
source§impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for DynSlice<'a, Dyn>
impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for DynSlice<'a, Dyn>
source§impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<&[Rhs]> for DynSlice<'a, Dyn>
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<&[Rhs]> for DynSlice<'a, Dyn>
source§impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<[Rhs]> for DynSlice<'a, Dyn>
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<[Rhs]> for DynSlice<'a, Dyn>
source§impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialOrd<Rhs> + ?Sized, Rhs> PartialOrd<&[Rhs]> for DynSlice<'a, Dyn>
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialOrd<Rhs> + ?Sized, Rhs> PartialOrd<&[Rhs]> for DynSlice<'a, Dyn>
Implements comparison of slices lexicographically.
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialOrd<Rhs> + ?Sized, Rhs> PartialOrd<[Rhs]> for DynSlice<'a, Dyn>
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialOrd<Rhs> + ?Sized, Rhs> PartialOrd<[Rhs]> for DynSlice<'a, Dyn>
Implements comparison of slices lexicographically.
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more