Struct dyn_slice::DynSliceMut

source ·
pub struct DynSliceMut<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>>(/* private fields */);
Expand description

&mut dyn [Trait]

A mutable type erased slice of elements that implement a trait.

§Example

use dyn_slice::standard::add_assign;

let mut array = [1, 2, 3, 4, 5];
let mut slice = add_assign::new_mut(&mut array);
slice.iter_mut().for_each(|x| *x += 10);
assert_eq!(array, [11, 12, 13, 14, 15]);

Implementations§

source§

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

source

pub unsafe fn with_vtable_ptr<DynSliceFromType>( value: &'a mut [DynSliceFromType], vtable_ptr: *const () ) -> Self

Construct a mutable dyn slice given a mutable 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 unsafe fn with_metadata<DynSliceFromType>( value: &'a mut [DynSliceFromType], metadata: DynMetadata<Dyn> ) -> Self

Construct a mutable dyn slice given a mutable 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: *mut () ) -> Self

Construct a mutable 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 const unsafe fn from_parts_with_metadata( metadata: DynMetadata<Dyn>, len: usize, data: *mut () ) -> Self

Construct a mutable 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 fn as_mut_ptr(&mut self) -> *mut ()

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

source

pub unsafe fn first_unchecked_mut(&mut self) -> &mut Dyn

Returns a mutable 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_mut(&mut self) -> Option<&mut Dyn>

Returns a mutable reference to the first element of the slice, or None if it is empty.

§Example
use dyn_slice::standard::add_assign;

let mut array = [1, 2, 3, 4, 5];
let mut slice = add_assign::new_mut(&mut array);

*slice.first_mut().unwrap() += 10;
assert_eq!(array, [11, 2, 3, 4, 5]);
source

pub fn last_mut(&mut self) -> Option<&mut Dyn>

Returns a mutable reference to the last element of the slice, or None if it is empty.

§Example
use dyn_slice::standard::add_assign;

let mut array = [1, 2, 3, 4, 5];
let mut slice = add_assign::new_mut(&mut array);

*slice.last_mut().unwrap() += 10;
assert_eq!(array, [1, 2, 3, 4, 15]);
source

pub fn get_mut(&mut self, index: usize) -> Option<&mut Dyn>

Returns a mutable reference to the element at the given index or None if the index is out of bounds.

§Example
use dyn_slice::standard::add_assign;

let mut array = [1, 2, 3, 4, 5];
let mut slice = add_assign::new_mut(&mut array);

*slice.get_mut(2).unwrap() += 10;
assert_eq!(array, [1, 2, 13, 4, 5]);
source

pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut Dyn

Returns a mutable 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_mut( &mut self, start: usize, len: usize ) -> DynSliceMut<'_, Dyn>

Get a mutable 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_mut<R: RangeBounds<usize>>( &mut self, range: R ) -> Option<DynSliceMut<'_, Dyn>>

Returns a mutable sub-slice from the start index with the len or None if the slice is out of bounds.

§Example
use dyn_slice::standard::add_assign;

let mut array = [1, 2, 3, 4, 5];
let mut slice = add_assign::new_mut(&mut array);

slice.slice_mut(1..4).unwrap().iter_mut().for_each(|x| *x += 10);
slice.slice_mut(2..).unwrap().iter_mut().for_each(|x| *x += 10);
slice.slice_mut(5..).unwrap().iter_mut().for_each(|x| *x += 10);
assert!(slice.slice(6..).is_none());
assert_eq!(array, [1, 12, 23, 24, 15]);
source

pub unsafe fn downcast_unchecked_mut<T>(&mut self) -> &mut [T]

Returns the underlying slice as &mut [T].

§Safety

The caller must ensure that the underlying slice is of type [T].

source

pub fn iter_mut(&mut self) -> IterMut<'_, Dyn>

Returns a mutable iterator over the slice.

§Example
use dyn_slice::standard::add_assign;

let mut array = [1, 2, 3, 4, 5];
let mut slice = add_assign::new_mut(&mut array);

slice.iter_mut().for_each(|x| *x += 10);
assert_eq!(array, [11, 12, 13, 14, 15]);
source§

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

source

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

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

source§

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

source

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

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

source§

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

source

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

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

Methods from Deref<Target = DynSlice<'a, Dyn>>§

source

pub 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 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 fn as_ptr(&self) -> *const ()

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

source

pub 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 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 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

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

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

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: Pointee<Metadata = DynMetadata<Dyn>> + Debug + ?Sized> Debug for DynSliceMut<'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>>> Deref for DynSliceMut<'a, Dyn>

§

type Target = DynSlice<'a, Dyn>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> Index<usize> for DynSliceMut<'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, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IndexMut<usize> for DynSliceMut<'a, Dyn>

source§

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

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

impl<'a, 'b, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for &'b DynSliceMut<'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, 'b, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for &'b mut DynSliceMut<'a, Dyn>

§

type IntoIter = IterMut<'b, Dyn>

Which kind of iterator are we turning this into?
§

type Item = &'b mut 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 DynSliceMut<'a, Dyn>

§

type IntoIter = IterMut<'a, Dyn>

Which kind of iterator are we turning this into?
§

type Item = &'a mut 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 DynSliceMut<'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 DynSliceMut<'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 DynSliceMut<'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 DynSliceMut<'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 DynSliceMut<'a, Dyn>

source§

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

Formats the value using the given formatter.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<'a, Dyn> UnwindSafe for DynSliceMut<'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, 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.