abi_stable::pointer_trait

Trait AsMutPtr

Source
pub unsafe trait AsMutPtr: AsPtr {
    // Required method
    fn as_mut_ptr(&mut self) -> *mut Self::PtrTarget;

    // Provided method
    fn as_rmut(&mut self) -> RMut<'_, Self::PtrTarget> { ... }
}
Expand description

For getting a mutable raw pointer to the value that this points to.

§Safety

The implementor of this trait must return a pointer to the same data as DerefMut::deref_mut, without constructing a &mut Self::Target in as_mut_ptr (or any function it calls).

The implementor of this trait must not override the defaulted methods.

§Example

use abi_stable::{
    erased_types::interfaces::DEIteratorInterface,
    pointer_trait::{
        PK_MutReference,
        AsPtr, AsMutPtr, CanTransmuteElement, GetPointerKind, TransmuteElement,
    },
    sabi_types::RMut,
    DynTrait,
};

fn main() {
    let mut iter = 0..=5;
    let reff: DynTrait<QuxMut<()>, DEIteratorInterface<_>> =
        DynTrait::from_ptr(QuxMut::new(&mut iter)).interface(DEIteratorInterface::NEW);
     
    assert_eq!(reff.collect::<Vec<u32>>(), [0, 1, 2, 3, 4, 5]);

    assert_eq!(iter.next(), None);
}


#[derive(Debug)]
#[repr(transparent)]
struct QuxMut<'a, T>(RMut<'a, T>);

impl<'a, T> QuxMut<'a, T> {
    pub fn new(reff: &'a mut T) -> Self {
        Self(RMut::new(reff))
    }
}

unsafe impl<T> GetPointerKind for QuxMut<'_, T> {
    type PtrTarget = T;
    type Kind = PK_MutReference;
}

unsafe impl<'a, T: 'a, U: 'a> CanTransmuteElement<U> for QuxMut<'a, T> {
    type TransmutedPtr = QuxMut<'a, U>;
     
    unsafe fn transmute_element_(self) -> Self::TransmutedPtr {
        QuxMut(self.0.transmute_element_())
    }
}

unsafe impl<T> AsPtr for QuxMut<'_, T> {
    fn as_ptr(&self) -> *const T {
        self.0.as_ptr()
    }
}

unsafe impl<T> AsMutPtr for QuxMut<'_, T> {
    fn as_mut_ptr(&mut self) -> *mut T {
        self.0.as_mut_ptr()
    }
}

Required Methods§

Source

fn as_mut_ptr(&mut self) -> *mut Self::PtrTarget

Gets a mutable raw pointer to the value that this points to.

Provided Methods§

Source

fn as_rmut(&mut self) -> RMut<'_, Self::PtrTarget>

Converts this pointer to an RRef.

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.

Implementors§

Source§

impl<'a, T> AsMutPtr for RMut<'a, T>

Source§

impl<T> AsMutPtr for RBox<T>

Source§

impl<T, Inline> AsMutPtr for RSmallBox<T, Inline>