Sovereign

Struct Sovereign 

Source
pub struct Sovereign<T> { /* private fields */ }
Expand description

A wrapper that enforces ownership semantics across network boundaries.

“Memory safety with sovereign integrity.”

Implementations§

Source§

impl<T> Sovereign<T>

Source

pub fn new(value: T) -> Self

Creates a new Sovereign resource under domestic jurisdiction.

§Atomic Ordering

Uses SeqCst ordering for maximum safety in distributed scenarios. This ensures all threads see state changes in a consistent order, which is critical for ownership semantics across network boundaries.

Source

pub fn new_exiled(value: T) -> Self

Creates a new Sovereign resource that is already under foreign jurisdiction.

This is useful for nodes receiving data that has been transferred from another node - the resource starts in an Exiled state.

§Example
use praborrow_core::{Sovereign, SovereignState};

let foreign_data = Sovereign::new_exiled(42i32);
assert!(foreign_data.is_exiled());
Source

pub fn annex(&self) -> Result<(), AnnexError>

Annexes the resource, moving it to foreign jurisdiction.

Once annexed, the resource cannot be accessed locally. Access attempts will result in a Sovereignty Violation.

Source

pub fn inner_ref(&self) -> &T

Returns a reference to the inner value without jurisdiction check.

§Safety

This is safe because we’re returning a shared reference and the caller is responsible for ensuring the resource is domestic. Use try_get() for safe access with jurisdiction verification.

Source

pub fn state(&self) -> SovereignState

Returns the current state of the resource.

Source

pub fn is_domestic(&self) -> bool

Returns true if the resource is under domestic jurisdiction.

Source

pub fn is_exiled(&self) -> bool

Returns true if the resource is under foreign jurisdiction (exiled).

Source

pub fn try_get(&self) -> Result<&T, SovereigntyError>

Attempts to get a reference to the value, returning an error if Exiled.

Source

pub fn try_get_mut(&mut self) -> Result<&mut T, SovereigntyError>

Attempts to get a mutable reference to the value, returning an error if Exiled.

Source

pub fn repatriate(&self, token: RepatriationToken)

Repatriates a resource, transitioning it from Exiled back to Domestic.

Requires a RepatriationToken as proof that the resource is safe to reclaim.

§Example
use praborrow_core::{Sovereign, SovereignState, RepatriationToken};

let resource = Sovereign::new(42i32);
resource.annex().unwrap();
assert!(resource.is_exiled());

// ... resource is sent to foreign node and returned ...

// SAFETY: construction of token implies safety verification
let token = unsafe { RepatriationToken::new(123) };
resource.repatriate(token);
assert!(resource.is_domestic());
Source

pub fn map<F, U>(&self, f: F) -> Result<U, SovereigntyError>
where F: FnOnce(&T) -> U,

Maps a function over the domestic sovereign value.

If the resource is Exiled, returns Err(SovereigntyError::ForeignJurisdiction) without evaluating the function.

§Example
use praborrow_core::Sovereign;
let s = Sovereign::new(5);
let result = s.map(|x| x * 2).unwrap();
assert_eq!(result, 10);
Source

pub fn and_then<F, U>(&self, f: F) -> Result<U, SovereigntyError>
where F: FnOnce(&T) -> Result<U, SovereigntyError>,

Chains a function that returns a Result over the domestic sovereign value.

This is useful for sequencing operations that might fail or themselves require jurisdiction checks.

Source

pub fn filter<P>(&self, predicate: P) -> Result<Option<&T>, SovereigntyError>
where P: FnOnce(&T) -> bool,

Returns a reference to the inner value if it matches the predicate.

§Returns
  • Ok(Some(&T)) if domestic and predicate is true
  • Ok(None) if domestic and predicate is false
  • Err(ForeignJurisdiction) if exiled
Source

pub fn modify<F>(&mut self, f: F) -> Result<(), SovereigntyError>
where F: FnOnce(&mut T),

Modifies the value in-place if domestic.

§Example
use praborrow_core::Sovereign;
let mut s = Sovereign::new(5);
s.modify(|x| *x += 1).unwrap();
assert_eq!(*s, 6);

Trait Implementations§

Source§

impl<T: Debug> Debug for Sovereign<T>

Source§

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

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

impl<T> Deref for Sovereign<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T> DerefMut for Sovereign<T>

Source§

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

Mutably dereferences the value.
Source§

impl<T> DistributedBorrow<T> for Sovereign<T>

Source§

fn try_hire( &self, candidate_id: u128, term: Duration, ) -> Result<Lease<T>, LeaseError>

Attempt to acquire a lease on the resource.
Source§

impl<T: Send> Send for Sovereign<T>

Source§

impl<T: Sync> Sync for Sovereign<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Sovereign<T>

§

impl<T> !RefUnwindSafe for Sovereign<T>

§

impl<T> Unpin for Sovereign<T>
where T: Unpin,

§

impl<T> UnwindSafe for Sovereign<T>
where T: UnwindSafe,

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

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more