Struct dyn_slice::DynSlice

source ·
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>

source

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.

source

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.

source

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 of DynMetadata transmuted, or optionally, a null pointer if len == 0,
  • len <= the length of the slice in memory from the data pointer,
  • data is a valid pointer to the slice,
  • the underlying slice is the same layout as [T]
source

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 of DynMetadata,
  • len <= the length of the slice in memory from the data pointer,
  • data is a valid pointer to the slice,
  • the underlying slice is the same layout as [T]
source

pub const fn vtable_ptr(&self) -> *const ()

Get the vtable pointer, which may be null if the slice is empty.

source

pub fn metadata(&self) -> Option<DynMetadata<Dyn>>

Get the metadata component of the element’s pointers, or possibly None if the slice is empty.

source

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);
source

pub const fn as_ptr(&self) -> *const ()

Returns a pointer to the underlying slice, which may be null if the slice is empty.

source

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());
source

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!

source

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
source

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
source

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
source

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!

source

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
source

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
source

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].

source

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!"]
source§

impl<'a> DynSlice<'a, dyn Any>

source

pub fn is<T: 'static>(&self) -> bool

Returns true if the underlying slice is of type T.

source

pub fn downcast<T: 'static>(&self) -> Option<&[T]>

Returns the underlying slice as &[T], or None if the underlying slice is not of type T.

source§

impl<'a> DynSlice<'a, dyn Any + Send>

source

pub fn is<T: 'static>(&self) -> bool

Returns true if the underlying slice is of type T.

source

pub fn downcast<T: 'static>(&self) -> Option<&[T]>

Returns the underlying slice as &[T], or None if the underlying slice is not of type T.

source§

impl<'a> DynSlice<'a, dyn Any + Sync + Send>

source

pub fn is<T: 'static>(&self) -> bool

Returns true if the underlying slice is of type T.

source

pub fn downcast<T: 'static>(&self) -> Option<&[T]>

Returns the underlying slice as &[T], or None if the underlying slice is not of type T.

Trait Implementations§

source§

impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> AsRef<DynSlice<'a, Dyn>> for DynSliceMut<'a, Dyn>

source§

fn as_ref(&self) -> &DynSlice<'a, Dyn>

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> Clone for DynSlice<'a, Dyn>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + Debug + ?Sized> Debug for DynSlice<'a, Dyn>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> Index<usize> for DynSlice<'a, Dyn>

§

type Output = Dyn

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<'a, 'b, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for &'b DynSlice<'a, Dyn>

§

type IntoIter = Iter<'b, Dyn>

Which kind of iterator are we turning this into?
§

type Item = &'b Dyn

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for DynSlice<'a, Dyn>

§

type IntoIter = Iter<'a, Dyn>

Which kind of iterator are we turning this into?
§

type Item = &'a Dyn

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<&[Rhs]> for DynSlice<'a, Dyn>

source§

fn eq(&self, other: &&[Rhs]) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<[Rhs]> for DynSlice<'a, Dyn>

source§

fn eq(&self, other: &[Rhs]) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialOrd<Rhs> + ?Sized, Rhs> PartialOrd<&[Rhs]> for DynSlice<'a, Dyn>

Implements comparison of slices lexicographically.

source§

fn partial_cmp(&self, other: &&[Rhs]) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialOrd<Rhs> + ?Sized, Rhs> PartialOrd<[Rhs]> for DynSlice<'a, Dyn>

Implements comparison of slices lexicographically.

source§

fn partial_cmp(&self, other: &[Rhs]) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + ?Sized> Pointer for DynSlice<'a, Dyn>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter.
source§

impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> Copy for DynSlice<'a, Dyn>

Auto Trait Implementations§

§

impl<'a, Dyn> Freeze for DynSlice<'a, Dyn>
where Dyn: ?Sized,

§

impl<'a, Dyn> RefUnwindSafe for DynSlice<'a, Dyn>
where Dyn: RefUnwindSafe + ?Sized,

§

impl<'a, Dyn> !Send for DynSlice<'a, Dyn>

§

impl<'a, Dyn> !Sync for DynSlice<'a, Dyn>

§

impl<'a, Dyn> Unpin for DynSlice<'a, Dyn>
where Dyn: ?Sized,

§

impl<'a, Dyn> UnwindSafe for DynSlice<'a, Dyn>
where Dyn: RefUnwindSafe + ?Sized,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, F> To<T> for F
where F: Into<T> + Copy,

source§

fn to(&self) -> T

Converts this reference into the (usually inferred) input type.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.