pub struct IdOwner { /* private fields */ }Expand description
A container for ExclusiveIds, allowing for explicit ownership management.
There is an implicit IdOwner for each thread, to which ExclusiveIds are associated when they are dropped,
and which can be queried via the local_ids constraint.
§Example
use triblespace_core::id::{IdOwner, ExclusiveId, fucid};
let mut owner = IdOwner::new();
let exclusive_id = fucid();
let id = owner.insert(exclusive_id);
assert!(owner.owns(&id));
assert_eq!(owner.take(&id), Some(ExclusiveId::force(id)));
assert!(!owner.owns(&id));Implementations§
Source§impl IdOwner
impl IdOwner
Sourcepub fn insert(&mut self, id: ExclusiveId) -> Id
pub fn insert(&mut self, id: ExclusiveId) -> Id
Inserts an ExclusiveId into the IdOwner, returning the underlying Id.
§Arguments
id- TheExclusiveIdto be inserted.
§Returns
The underlying Id.
Sourcepub fn defer_insert(&self, id: ExclusiveId) -> OwnedId<'_>
pub fn defer_insert(&self, id: ExclusiveId) -> OwnedId<'_>
Defers inserting an ExclusiveId into the IdOwner, returning an OwnedId.
The OwnedId will return the ExclusiveId to the IdOwner when dropped.
This is useful if you generated an ExclusiveId that you want to use temporarily,
but want to make sure it is returned to the IdOwner when you are done.
§Arguments
id- TheExclusiveIdto be inserted.
§Returns
An OwnedId that will return the ExclusiveId to the IdOwner when dropped.
§Example
use triblespace_core::prelude::*;
use valueschemas::ShortString;
use triblespace_core::id_hex;
let mut owner = IdOwner::new();
let owned_id = owner.defer_insert(fucid());
let trible = Trible::new(&owned_id, &id_hex!("7830D7B3C2DCD44EB3FA68C93D06B973"), &ShortString::value_from("Hello, World!"));Sourcepub fn force_insert(&self, id: &Id)
pub fn force_insert(&self, id: &Id)
Forces an Id into the IdOwner as an ExclusiveId.
§Arguments
id- TheIdto be forced into anExclusiveId.
Sourcepub fn take(&self, id: &Id) -> Option<ExclusiveId>
pub fn take(&self, id: &Id) -> Option<ExclusiveId>
Takes an Id from the IdOwner, returning it as an ExclusiveId.
§Arguments
id- TheIdto be taken.
§Returns
An ExclusiveId if the Id was found, otherwise None.
Sourcepub fn borrow<'a>(&'a self, id: &Id) -> Option<OwnedId<'a>>
pub fn borrow<'a>(&'a self, id: &Id) -> Option<OwnedId<'a>>
Get an OwnedId from the IdOwner.
The OwnedId will return the ExclusiveId to the IdOwner when dropped.
This is useful for temporary exclusive access to an Id.
If you want to keep the Id for longer, you can use the take method,
but you will have to manually return it to the IdOwner when you are done.
§Arguments
id- TheIdto be taken.
§Returns
An OwnedId if the Id was found, otherwise None.
§Example
use triblespace_core::id::{IdOwner, ExclusiveId, fucid};
let mut owner = IdOwner::new();
let exclusive_id = fucid();
let id = owner.insert(exclusive_id);
{
let mut owned_id = owner.borrow(&id).unwrap();
assert_eq!(owned_id.id, id);
assert!(!owner.owns(&id));
}
assert!(owner.owns(&id));Trait Implementations§
Source§impl ContainsConstraint<'static, GenId> for &IdOwner
impl ContainsConstraint<'static, GenId> for &IdOwner
Source§type Constraint = <PATCH<ID_LEN> as ContainsConstraint<'static, GenId>>::Constraint
type Constraint = <PATCH<ID_LEN> as ContainsConstraint<'static, GenId>>::Constraint
has.Auto Trait Implementations§
impl !Freeze for IdOwner
impl !RefUnwindSafe for IdOwner
impl Send for IdOwner
impl !Sync for IdOwner
impl Unpin for IdOwner
impl UnsafeUnpin for IdOwner
impl UnwindSafe for IdOwner
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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more