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>
impl<T> Sovereign<T>
Sourcepub fn new(value: T) -> Self
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.
Sourcepub fn new_exiled(value: T) -> Self
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());Sourcepub fn annex(&self) -> Result<(), AnnexError>
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.
Sourcepub fn inner_ref(&self) -> &T
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.
Sourcepub fn state(&self) -> SovereignState
pub fn state(&self) -> SovereignState
Returns the current state of the resource.
Sourcepub fn is_domestic(&self) -> bool
pub fn is_domestic(&self) -> bool
Returns true if the resource is under domestic jurisdiction.
Sourcepub fn is_exiled(&self) -> bool
pub fn is_exiled(&self) -> bool
Returns true if the resource is under foreign jurisdiction (exiled).
Sourcepub fn try_get(&self) -> Result<&T, SovereigntyError>
pub fn try_get(&self) -> Result<&T, SovereigntyError>
Attempts to get a reference to the value, returning an error if Exiled.
Sourcepub fn try_get_mut(&mut self) -> Result<&mut T, SovereigntyError>
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.
Sourcepub fn repatriate(&self, token: RepatriationToken)
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());Sourcepub fn map<F, U>(&self, f: F) -> Result<U, SovereigntyError>
pub fn map<F, U>(&self, f: F) -> Result<U, SovereigntyError>
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);Sourcepub fn and_then<F, U>(&self, f: F) -> Result<U, SovereigntyError>
pub fn and_then<F, U>(&self, f: F) -> 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.