Skip to main content

IdOwner

Struct IdOwner 

Source
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

Source

pub fn new() -> Self

Creates a new IdOwner.

This is typically not necessary, as each thread has an implicit IdOwner associated with it.

§Returns

A new IdOwner.

Source

pub fn insert(&mut self, id: ExclusiveId) -> Id

Inserts an ExclusiveId into the IdOwner, returning the underlying Id.

§Arguments
§Returns

The underlying Id.

Source

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
§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!"));
Source

pub fn force_insert(&self, id: &Id)

Forces an Id into the IdOwner as an ExclusiveId.

§Arguments
Source

pub fn take(&self, id: &Id) -> Option<ExclusiveId>

Takes an Id from the IdOwner, returning it as an ExclusiveId.

§Arguments
  • id - The Id to be taken.
§Returns

An ExclusiveId if the Id was found, otherwise None.

Source

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 - The Id to 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));
Source

pub fn owns(&self, id: &Id) -> bool

Checks if the IdOwner owns an Id.

§Arguments
  • id - The Id to be checked.
§Returns

true if the Id is owned by the IdOwner, otherwise false.

Trait Implementations§

Source§

impl ContainsConstraint<'static, GenId> for &IdOwner

Source§

type Constraint = <PATCH<ID_LEN> as ContainsConstraint<'static, GenId>>::Constraint

The concrete constraint type produced by has.
Source§

fn has(self, v: Variable<GenId>) -> Self::Constraint

Create a constraint that filters the values assigned to the variable to only those that are contained in the collection. Read more
Source§

impl Default for IdOwner

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V