pub struct Irc<T: IrcItem<Tag>, Tag> { /* private fields */ }Available on crate feature
irc only.Expand description
Intrusive reference counter, which support conversion bwteween Box<T>.
It does not support weak reference.
Implementations§
Source§impl<T: IrcItem<Tag>, Tag> Irc<T, Tag>
impl<T: IrcItem<Tag>, Tag> Irc<T, Tag>
Sourcepub fn new(inner: T) -> Self
pub fn new(inner: T) -> Self
Wrap a stack value T into Irc.
The counter will be reset to 1 on initialization.
pub fn ptr_eq(this: &Self, other: &Self) -> bool
Sourcepub fn is_unique(&self) -> bool
pub fn is_unique(&self) -> bool
If is_unique returns true, then this thread is the only owner
§False negative
it’s possible to return false when counter drop to 1, Because of using Acquire load and Release on drop.
§Example
use embed_collections::irc::{Irc, IrcItem};
use core::sync::atomic::AtomicUsize;
struct Tag;
struct MyItem {
value: i32,
counter: AtomicUsize,
}
unsafe impl IrcItem<Tag> for MyItem {
type Counter = AtomicUsize;
fn counter(&self) -> &Self::Counter {
&self.counter
}
}
// Create a new Irc
let irc1 = Irc::<_, Tag>::new(MyItem { value: 10, counter: AtomicUsize::new(0) });
assert_eq!(irc1.value, 10);
assert!(irc1.is_unique());
// Clone the Irc
let irc2 = irc1.clone();
assert_eq!(irc1.strong_count(), 2);
assert!(!irc1.is_unique());Source§impl<T: IrcItem<Tag> + Clone, Tag> Irc<T, Tag>
impl<T: IrcItem<Tag> + Clone, Tag> Irc<T, Tag>
Sourcepub fn make_mut(this: &mut Self) -> &mut T
pub fn make_mut(this: &mut Self) -> &mut T
The Cow function, the same as Arc::make_mut()
§Example
use embed_collections::irc::{Irc, IrcItem};
use core::sync::atomic::AtomicUsize;
struct Tag;
struct MyItem {
value: i32,
counter: AtomicUsize,
}
impl Clone for MyItem {
fn clone(&self) -> Self {
Self { value: self.value, counter: AtomicUsize::new(0) }
}
}
unsafe impl IrcItem<Tag> for MyItem {
type Counter = AtomicUsize;
fn counter(&self) -> &Self::Counter {
&self.counter
}
}
let mut irc1 = Irc::<_, Tag>::new(MyItem { value: 10, counter: AtomicUsize::new(0) });
let irc2 = irc1.clone();
// This will clone the inner item because it's shared
let m = Irc::make_mut(&mut irc1);
m.value = 20;
assert_eq!(irc1.value, 20);
assert_eq!(irc2.value, 10);Trait Implementations§
impl<T: IrcItem<Tag>, Tag> Send for Irc<T, Tag>
impl<T: IrcItem<Tag>, Tag> Sync for Irc<T, Tag>
Auto Trait Implementations§
impl<T, Tag> Freeze for Irc<T, Tag>
impl<T, Tag> RefUnwindSafe for Irc<T, Tag>where
T: RefUnwindSafe,
impl<T, Tag> Unpin for Irc<T, Tag>
impl<T, Tag> UnsafeUnpin for Irc<T, Tag>
impl<T, Tag> UnwindSafe for Irc<T, Tag>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more