pub struct Imp<T: ?Sized> { /* private fields */ }Expand description
§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>
impl<T> Imp<T>
Sourcepub unsafe fn new(t: T) -> Self
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.
Sourcepub fn ptr_eq(this: &Self, other: &Self) -> bool
pub fn ptr_eq(this: &Self, other: &Self) -> bool
Returns true if two pointers are equal
§Arguments
this- A pointer to compareother- 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 falseTrait Implementations§
Source§impl<T: AddAssign<T> + Copy + AddAssign> AddAssign<T> for Imp<T>
impl<T: AddAssign<T> + Copy + AddAssign> AddAssign<T> for Imp<T>
Source§fn add_assign(&mut self, other: T)
fn add_assign(&mut self, other: T)
+= operation. Read moreSource§impl<T: AddAssign<T> + Copy + AddAssign> AddAssign for Imp<T>
impl<T: AddAssign<T> + Copy + AddAssign> AddAssign for Imp<T>
Source§fn add_assign(&mut self, other: Imp<T>)
fn add_assign(&mut self, other: Imp<T>)
+= operation. Read moreSource§impl<T: BitAndAssign<T> + Copy + BitAndAssign> BitAndAssign<T> for Imp<T>
impl<T: BitAndAssign<T> + Copy + BitAndAssign> BitAndAssign<T> for Imp<T>
Source§fn bitand_assign(&mut self, other: T)
fn bitand_assign(&mut self, other: T)
&= operation. Read moreSource§impl<T: BitAndAssign<T> + Copy + BitAndAssign> BitAndAssign for Imp<T>
impl<T: BitAndAssign<T> + Copy + BitAndAssign> BitAndAssign for Imp<T>
Source§fn bitand_assign(&mut self, other: Imp<T>)
fn bitand_assign(&mut self, other: Imp<T>)
&= operation. Read moreSource§impl<T: BitOrAssign<T> + Copy + BitOrAssign> BitOrAssign<T> for Imp<T>
impl<T: BitOrAssign<T> + Copy + BitOrAssign> BitOrAssign<T> for Imp<T>
Source§fn bitor_assign(&mut self, other: T)
fn bitor_assign(&mut self, other: T)
|= operation. Read moreSource§impl<T: BitOrAssign<T> + Copy + BitOrAssign> BitOrAssign for Imp<T>
impl<T: BitOrAssign<T> + Copy + BitOrAssign> BitOrAssign for Imp<T>
Source§fn bitor_assign(&mut self, other: Imp<T>)
fn bitor_assign(&mut self, other: Imp<T>)
|= operation. Read moreSource§impl<T: BitXorAssign<T> + Copy + BitXorAssign> BitXorAssign<T> for Imp<T>
impl<T: BitXorAssign<T> + Copy + BitXorAssign> BitXorAssign<T> for Imp<T>
Source§fn bitxor_assign(&mut self, other: T)
fn bitxor_assign(&mut self, other: T)
^= operation. Read moreSource§impl<T: BitXorAssign<T> + Copy + BitXorAssign> BitXorAssign for Imp<T>
impl<T: BitXorAssign<T> + Copy + BitXorAssign> BitXorAssign for Imp<T>
Source§fn bitxor_assign(&mut self, other: Imp<T>)
fn bitxor_assign(&mut self, other: Imp<T>)
^= operation. Read moreSource§impl<T: DivAssign<T> + Copy + DivAssign> DivAssign<T> for Imp<T>
impl<T: DivAssign<T> + Copy + DivAssign> DivAssign<T> for Imp<T>
Source§fn div_assign(&mut self, other: T)
fn div_assign(&mut self, other: T)
/= operation. Read moreSource§impl<T: DivAssign<T> + Copy + DivAssign> DivAssign for Imp<T>
impl<T: DivAssign<T> + Copy + DivAssign> DivAssign for Imp<T>
Source§fn div_assign(&mut self, other: Imp<T>)
fn div_assign(&mut self, other: Imp<T>)
/= operation. Read moreSource§impl<T: MulAssign<T> + Copy + MulAssign> MulAssign<T> for Imp<T>
impl<T: MulAssign<T> + Copy + MulAssign> MulAssign<T> for Imp<T>
Source§fn mul_assign(&mut self, other: T)
fn mul_assign(&mut self, other: T)
*= operation. Read moreSource§impl<T: MulAssign<T> + Copy + MulAssign> MulAssign for Imp<T>
impl<T: MulAssign<T> + Copy + MulAssign> MulAssign for Imp<T>
Source§fn mul_assign(&mut self, other: Imp<T>)
fn mul_assign(&mut self, other: Imp<T>)
*= operation. Read moreSource§impl<T: Ord> Ord for Imp<T>
impl<T: Ord> Ord for Imp<T>
Source§impl<T: PartialOrd> PartialOrd<T> for Imp<T>
impl<T: PartialOrd> PartialOrd<T> for Imp<T>
Source§impl<T: PartialOrd> PartialOrd for Imp<T>
impl<T: PartialOrd> PartialOrd for Imp<T>
Source§impl<T: RemAssign<T> + Copy + RemAssign> RemAssign<T> for Imp<T>
impl<T: RemAssign<T> + Copy + RemAssign> RemAssign<T> for Imp<T>
Source§fn rem_assign(&mut self, other: T)
fn rem_assign(&mut self, other: T)
%= operation. Read moreSource§impl<T: RemAssign<T> + Copy + RemAssign> RemAssign for Imp<T>
impl<T: RemAssign<T> + Copy + RemAssign> RemAssign for Imp<T>
Source§fn rem_assign(&mut self, other: Imp<T>)
fn rem_assign(&mut self, other: Imp<T>)
%= operation. Read moreSource§impl<T: ShlAssign<T> + Copy + ShlAssign> ShlAssign<T> for Imp<T>
impl<T: ShlAssign<T> + Copy + ShlAssign> ShlAssign<T> for Imp<T>
Source§fn shl_assign(&mut self, other: T)
fn shl_assign(&mut self, other: T)
<<= operation. Read moreSource§impl<T: ShlAssign<T> + Copy + ShlAssign> ShlAssign for Imp<T>
impl<T: ShlAssign<T> + Copy + ShlAssign> ShlAssign for Imp<T>
Source§fn shl_assign(&mut self, other: Imp<T>)
fn shl_assign(&mut self, other: Imp<T>)
<<= operation. Read moreSource§impl<T: ShrAssign<T> + Copy + ShrAssign> ShrAssign<T> for Imp<T>
impl<T: ShrAssign<T> + Copy + ShrAssign> ShrAssign<T> for Imp<T>
Source§fn shr_assign(&mut self, other: T)
fn shr_assign(&mut self, other: T)
>>= operation. Read moreSource§impl<T: ShrAssign<T> + Copy + ShrAssign> ShrAssign for Imp<T>
impl<T: ShrAssign<T> + Copy + ShrAssign> ShrAssign for Imp<T>
Source§fn shr_assign(&mut self, other: Imp<T>)
fn shr_assign(&mut self, other: Imp<T>)
>>= operation. Read moreSource§impl<T: SubAssign<T> + Copy + SubAssign> SubAssign<T> for Imp<T>
impl<T: SubAssign<T> + Copy + SubAssign> SubAssign<T> for Imp<T>
Source§fn sub_assign(&mut self, other: T)
fn sub_assign(&mut self, other: T)
-= operation. Read moreSource§impl<T: SubAssign<T> + Copy + SubAssign> SubAssign for Imp<T>
impl<T: SubAssign<T> + Copy + SubAssign> SubAssign for Imp<T>
Source§fn sub_assign(&mut self, other: Imp<T>)
fn sub_assign(&mut self, other: Imp<T>)
-= operation. Read more