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>
impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> DynSliceMut<'a, Dyn>
sourcepub unsafe fn with_vtable_ptr<DynSliceFromType>(
value: &'a mut [DynSliceFromType],
vtable_ptr: *const ()
) -> Self
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
.
sourcepub unsafe fn with_metadata<DynSliceFromType>(
value: &'a mut [DynSliceFromType],
metadata: DynMetadata<Dyn>
) -> Self
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
.
sourcepub const unsafe fn from_parts(
vtable_ptr: *const (),
len: usize,
data: *mut ()
) -> Self
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 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 const unsafe fn from_parts_with_metadata(
metadata: DynMetadata<Dyn>,
len: usize,
data: *mut ()
) -> Self
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 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 fn as_mut_ptr(&mut self) -> *mut ()
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.
sourcepub unsafe fn first_unchecked_mut(&mut self) -> &mut Dyn
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!
sourcepub fn first_mut(&mut self) -> Option<&mut Dyn>
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]);
sourcepub fn last_mut(&mut self) -> Option<&mut Dyn>
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]);
sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut Dyn>
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]);
sourcepub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut Dyn
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!
sourcepub unsafe fn slice_unchecked_mut(
&mut self,
start: usize,
len: usize
) -> DynSliceMut<'_, Dyn>
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
sourcepub fn slice_mut<R: RangeBounds<usize>>(
&mut self,
range: R
) -> Option<DynSliceMut<'_, Dyn>>
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]);
sourcepub unsafe fn downcast_unchecked_mut<T>(&mut self) -> &mut [T]
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]
.
sourcepub fn iter_mut(&mut self) -> IterMut<'_, Dyn> ⓘ
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>
impl<'a> DynSliceMut<'a, dyn Any>
sourcepub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut [T]>
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>
impl<'a> DynSliceMut<'a, dyn Any + Send>
sourcepub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut [T]>
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>
impl<'a> DynSliceMut<'a, dyn Any + Sync + Send>
sourcepub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut [T]>
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>>§
sourcepub fn vtable_ptr(&self) -> *const ()
pub 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 fn len(&self) -> usize
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);
sourcepub fn as_ptr(&self) -> *const ()
pub fn as_ptr(&self) -> *const ()
Returns a pointer to the underlying slice, which may be null if the slice is empty.
sourcepub fn is_empty(&self) -> bool
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());
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 unsafe fn downcast_unchecked<T>(&self) -> &[T]
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]
.
sourcepub fn iter(&self) -> Iter<'_, Dyn> ⓘ
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!"]
sourcepub fn downcast<T: 'static>(&self) -> Option<&[T]>
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>
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 DynSliceMut<'a, Dyn>
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + Debug + ?Sized> Debug for DynSliceMut<'a, Dyn>
source§impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> Deref for DynSliceMut<'a, Dyn>
impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> Deref for DynSliceMut<'a, Dyn>
source§impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> Index<usize> for DynSliceMut<'a, Dyn>
impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> Index<usize> for DynSliceMut<'a, Dyn>
source§impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IndexMut<usize> for DynSliceMut<'a, Dyn>
impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IndexMut<usize> for DynSliceMut<'a, Dyn>
source§impl<'a, 'b, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for &'b DynSliceMut<'a, Dyn>
impl<'a, 'b, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for &'b DynSliceMut<'a, Dyn>
source§impl<'a, 'b, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for &'b mut DynSliceMut<'a, Dyn>
impl<'a, 'b, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for &'b mut DynSliceMut<'a, Dyn>
source§impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for DynSliceMut<'a, Dyn>
impl<'a, Dyn: ?Sized + Pointee<Metadata = DynMetadata<Dyn>>> IntoIterator for DynSliceMut<'a, Dyn>
source§impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<&[Rhs]> for DynSliceMut<'a, Dyn>
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<&[Rhs]> for DynSliceMut<'a, Dyn>
source§impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<[Rhs]> for DynSliceMut<'a, Dyn>
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialEq<Rhs> + ?Sized, Rhs> PartialEq<[Rhs]> for DynSliceMut<'a, Dyn>
source§impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialOrd<Rhs> + ?Sized, Rhs> PartialOrd<&[Rhs]> for DynSliceMut<'a, Dyn>
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialOrd<Rhs> + ?Sized, Rhs> PartialOrd<&[Rhs]> for DynSliceMut<'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 DynSliceMut<'a, Dyn>
impl<'a, Dyn: Pointee<Metadata = DynMetadata<Dyn>> + PartialOrd<Rhs> + ?Sized, Rhs> PartialOrd<[Rhs]> for DynSliceMut<'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