Imp

Struct Imp 

Source
pub struct Imp<T: ?Sized> { /* private fields */ }
Expand description

crates.io downloads docs.rs

§Safety

The DerefMut implementation is unsound due to this library essentially working around the runtime safety provided by using RefCell. See Issue #2.
Due to this Imp::new(..) has been marked as unsafe.

§Interior Mutability Pointer

A wrapper around Rc<RefCell<T>> allowing immediate access to inner methods, without the need for .borrow() or .borrow_mut(), allowing for a more seamless pointer experience.

let mut k = Imp::new(String::new());
let p = k.clone(); // Clone the pointer.
k.push_str("yo");
println!("{} {}", k, p); // Prints "yo yo"

Also allows the use of operators:

let mut k = Imp::new(5);
let p = k.clone(); // Clone the pointer.
k += 5;
println!("{} {}", k, p); // Prints "10 10"

The biggest difference to Rc<RefCell<T>> is that your pointer instance will need to be marked as mut if you want to use &mut self methods, as opposed to Rc<RefCell<T>> instances where you can call .borrow_mut(), removing the need for the mut keyword. However, this does not mean that all clones of the pointer need to be mutable!

let k = Imp::new(String::new());
let mut p = k.clone(); // Clone the pointer.
p.push_str("yo");
println!("{:?} {:?}", k, p); // Prints "yo yo"

Also supports dynamic dispatch for all your trait ojects, in both mutable and inmutable contexts!

trait Animal {
    fn sound(&self) -> &'static str;
    fn volume(&self) -> i32;
    fn set_volume(&mut self, v: i32);
}

#[derive(Clone, Copy)]
struct Sheep {
    volume: i32,
}
impl Animal for Sheep {
    fn sound(&self) -> &'static str {
        "baah"
    }

    fn volume(&self) -> i32 {
        self.volume
    }

    fn set_volume(&mut self, v: i32) {
        self.volume = v;
    }
}

#[derive(Clone, Copy)]
struct Dog {
    volume: i32,
}
impl Animal for Dog {
    fn sound(&self) -> &'static str {
        "bark"
    }

    fn volume(&self) -> i32 {
        self.volume
    }

    fn set_volume(&mut self, v: i32) {
        self.volume = v;
    }
}
let s = Sheep { volume: 10 };
let d = Dog { volume: 15 };

let mut rc_refcell: Vec<Rc<RefCell<dyn Animal>>> =
    vec![Rc::new(RefCell::new(s)), Rc::new(RefCell::new(d))];
let mut imp: Vec<Imp<dyn Animal>> = vec![Imp::new(s), Imp::new(d)];

rc_refcell.iter_mut().for_each(|a| {
    let v = a.borrow().volume();
    a.borrow_mut().set_volume(v * 2);
});

imp.iter_mut().for_each(|a| {
    let v = a.volume();
    a.set_volume(v * 2);
});

let rc_refcell = rc_refcell
    .iter()
    .map(|p| p.borrow().volume())
    .collect::<Vec<_>>();
let imp = imp.iter().map(|p| p.volume()).collect::<Vec<_>>();

println!("{:?}", rc_refcell); // Prints [20, 30]
println!("{:?}", imp);        // Prints [20, 30]

Implementations§

Source§

impl<T> Imp<T>

Source

pub unsafe fn new(t: T) -> Self

Returns a pointer to the data

§Arguments
  • t - The value to be pointed to.
§Examples
use interior_mutability_pointer::Imp;
let mut p = unsafe { Imp::new(String::new())};
let p2 = p.clone();
p.push_str("yoo"); // Modifies the inner value of both p and p2.
§Safety

DerefMut implementation is unsound due to this library essentially working around the runtime safety provided by using RefCell. See Issue #2.

Source

pub fn ptr_eq(this: &Self, other: &Self) -> bool

Returns true if two pointers are equal

§Arguments
  • this - A pointer to compare
  • other - The other pointer to compare to
§Examples
use interior_mutability_pointer::Imp;
let p1 = unsafe { Imp::new(String::new()) };
let p2 = p1.clone();
let p3 = unsafe { Imp::new(String::new()) };
println!("{}", Imp::ptr_eq(&p1, &p2)); // Prints true
println!("{}", Imp::ptr_eq(&p1, &p3)); // Prints false

Trait Implementations§

Source§

impl<T: Add<T> + Copy + Add<Output = T>> Add<T> for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: T) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: Add<T> + Copy + Add<Output = T>> Add for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: AddAssign<T> + Copy + AddAssign> AddAssign<T> for Imp<T>

Source§

fn add_assign(&mut self, other: T)

Performs the += operation. Read more
Source§

impl<T: AddAssign<T> + Copy + AddAssign> AddAssign for Imp<T>

Source§

fn add_assign(&mut self, other: Imp<T>)

Performs the += operation. Read more
Source§

impl<T: BitAnd<T> + Copy + BitAnd<Output = T>> BitAnd<T> for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: T) -> Self::Output

Performs the & operation. Read more
Source§

impl<T: BitAnd<T> + Copy + BitAnd<Output = T>> BitAnd for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl<T: BitAndAssign<T> + Copy + BitAndAssign> BitAndAssign<T> for Imp<T>

Source§

fn bitand_assign(&mut self, other: T)

Performs the &= operation. Read more
Source§

impl<T: BitAndAssign<T> + Copy + BitAndAssign> BitAndAssign for Imp<T>

Source§

fn bitand_assign(&mut self, other: Imp<T>)

Performs the &= operation. Read more
Source§

impl<T: BitOr<T> + Copy + BitOr<Output = T>> BitOr<T> for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: T) -> Self::Output

Performs the | operation. Read more
Source§

impl<T: BitOr<T> + Copy + BitOr<Output = T>> BitOr for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl<T: BitOrAssign<T> + Copy + BitOrAssign> BitOrAssign<T> for Imp<T>

Source§

fn bitor_assign(&mut self, other: T)

Performs the |= operation. Read more
Source§

impl<T: BitOrAssign<T> + Copy + BitOrAssign> BitOrAssign for Imp<T>

Source§

fn bitor_assign(&mut self, other: Imp<T>)

Performs the |= operation. Read more
Source§

impl<T: BitXor<T> + Copy + BitXor<Output = T>> BitXor<T> for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: T) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<T: BitXor<T> + Copy + BitXor<Output = T>> BitXor for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: Self) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<T: BitXorAssign<T> + Copy + BitXorAssign> BitXorAssign<T> for Imp<T>

Source§

fn bitxor_assign(&mut self, other: T)

Performs the ^= operation. Read more
Source§

impl<T: BitXorAssign<T> + Copy + BitXorAssign> BitXorAssign for Imp<T>

Source§

fn bitxor_assign(&mut self, other: Imp<T>)

Performs the ^= operation. Read more
Source§

impl<T: ?Sized> Clone for Imp<T>

Source§

fn clone(&self) -> Self

Returns a duplicate 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<T: Debug> Debug for Imp<T>

Source§

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

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

impl<T: ?Sized> Deref for Imp<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T: ?Sized> DerefMut for Imp<T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: Display> Display for Imp<T>

Source§

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

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

impl<T: Div<T> + Copy + Div<Output = T>> Div<T> for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the / operator.
Source§

fn div(self, other: T) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: Div<T> + Copy + Div<Output = T>> Div for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: DivAssign<T> + Copy + DivAssign> DivAssign<T> for Imp<T>

Source§

fn div_assign(&mut self, other: T)

Performs the /= operation. Read more
Source§

impl<T: DivAssign<T> + Copy + DivAssign> DivAssign for Imp<T>

Source§

fn div_assign(&mut self, other: Imp<T>)

Performs the /= operation. Read more
Source§

impl<T: Index<Range<usize>>> Index<Range<usize>> for Imp<T>

Source§

type Output = <T as Index<Range<usize>>>::Output

The returned type after indexing.
Source§

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

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

impl<T: Index<usize>> Index<usize> for Imp<T>

Source§

type Output = <T as Index<usize>>::Output

The returned type after indexing.
Source§

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

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

impl<T: IndexMut<Range<usize>>> IndexMut<Range<usize>> for Imp<T>

Source§

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

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

impl<T: IndexMut<usize>> IndexMut<usize> for Imp<T>

Source§

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

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

impl<T: Mul<T> + Copy + Mul<Output = T>> Mul<T> for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: T) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: Mul<T> + Copy + Mul<Output = T>> Mul for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: MulAssign<T> + Copy + MulAssign> MulAssign<T> for Imp<T>

Source§

fn mul_assign(&mut self, other: T)

Performs the *= operation. Read more
Source§

impl<T: MulAssign<T> + Copy + MulAssign> MulAssign for Imp<T>

Source§

fn mul_assign(&mut self, other: Imp<T>)

Performs the *= operation. Read more
Source§

impl<T: Neg + Copy> Neg for Imp<T>

Source§

type Output = Imp<<T as Neg>::Output>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<T: Not + Copy> Not for Imp<T>

Source§

type Output = Imp<<T as Not>::Output>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl<T: Ord> Ord for Imp<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq<T> for Imp<T>

Source§

fn eq(&self, other: &T) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialEq> PartialEq for Imp<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd<T> for Imp<T>

Source§

fn partial_cmp(&self, other: &T) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: PartialOrd> PartialOrd for Imp<T>

Source§

fn partial_cmp(&self, other: &Self) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Rem<T> + Copy + Rem<Output = T>> Rem<T> for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the % operator.
Source§

fn rem(self, other: T) -> Self::Output

Performs the % operation. Read more
Source§

impl<T: Rem<T> + Copy + Rem<Output = T>> Rem for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the % operator.
Source§

fn rem(self, other: Self) -> Self::Output

Performs the % operation. Read more
Source§

impl<T: RemAssign<T> + Copy + RemAssign> RemAssign<T> for Imp<T>

Source§

fn rem_assign(&mut self, other: T)

Performs the %= operation. Read more
Source§

impl<T: RemAssign<T> + Copy + RemAssign> RemAssign for Imp<T>

Source§

fn rem_assign(&mut self, other: Imp<T>)

Performs the %= operation. Read more
Source§

impl<T: Shl<T> + Copy + Shl<Output = T>> Shl<T> for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the << operator.
Source§

fn shl(self, other: T) -> Self::Output

Performs the << operation. Read more
Source§

impl<T: Shl<T> + Copy + Shl<Output = T>> Shl for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the << operator.
Source§

fn shl(self, other: Self) -> Self::Output

Performs the << operation. Read more
Source§

impl<T: ShlAssign<T> + Copy + ShlAssign> ShlAssign<T> for Imp<T>

Source§

fn shl_assign(&mut self, other: T)

Performs the <<= operation. Read more
Source§

impl<T: ShlAssign<T> + Copy + ShlAssign> ShlAssign for Imp<T>

Source§

fn shl_assign(&mut self, other: Imp<T>)

Performs the <<= operation. Read more
Source§

impl<T: Shr<T> + Copy + Shr<Output = T>> Shr<T> for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the >> operator.
Source§

fn shr(self, other: T) -> Self::Output

Performs the >> operation. Read more
Source§

impl<T: Shr<T> + Copy + Shr<Output = T>> Shr for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the >> operator.
Source§

fn shr(self, other: Self) -> Self::Output

Performs the >> operation. Read more
Source§

impl<T: ShrAssign<T> + Copy + ShrAssign> ShrAssign<T> for Imp<T>

Source§

fn shr_assign(&mut self, other: T)

Performs the >>= operation. Read more
Source§

impl<T: ShrAssign<T> + Copy + ShrAssign> ShrAssign for Imp<T>

Source§

fn shr_assign(&mut self, other: Imp<T>)

Performs the >>= operation. Read more
Source§

impl<T: Sub<T> + Copy + Sub<Output = T>> Sub<T> for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: T) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: Sub<T> + Copy + Sub<Output = T>> Sub for Imp<T>

Source§

type Output = Imp<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: SubAssign<T> + Copy + SubAssign> SubAssign<T> for Imp<T>

Source§

fn sub_assign(&mut self, other: T)

Performs the -= operation. Read more
Source§

impl<T: SubAssign<T> + Copy + SubAssign> SubAssign for Imp<T>

Source§

fn sub_assign(&mut self, other: Imp<T>)

Performs the -= operation. Read more
Source§

impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Imp<U>> for Imp<T>

Source§

impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Imp<U>> for Imp<T>

Source§

impl<T: Eq> Eq for Imp<T>

Auto Trait Implementations§

§

impl<T> Freeze for Imp<T>
where T: ?Sized,

§

impl<T> !RefUnwindSafe for Imp<T>

§

impl<T> !Send for Imp<T>

§

impl<T> !Sync for Imp<T>

§

impl<T> Unpin for Imp<T>
where T: ?Sized,

§

impl<T> !UnwindSafe for Imp<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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

Source§

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.