pub struct PrivacyBudget { /* private fields */ }Expand description
Thread-safe differential-privacy budget tracker.
Epsilon and delta consumption are tracked with AtomicU64 using the
f64::to_bits / f64::from_bits trick, so that the struct can be
shared across threads without a Mutex.
Implementations§
Source§impl PrivacyBudget
impl PrivacyBudget
Sourcepub fn new(epsilon_total: f64, delta_total: f64) -> Result<Self, BudgetError>
pub fn new(epsilon_total: f64, delta_total: f64) -> Result<Self, BudgetError>
Create a new budget.
§Errors
Returns BudgetError::InvalidBudget if:
epsilon_total≤ 0delta_total≤ 0delta_total≥ 1
Sourcepub fn epsilon_total(&self) -> f64
pub fn epsilon_total(&self) -> f64
Total epsilon budget (immutable).
Sourcepub fn delta_total(&self) -> f64
pub fn delta_total(&self) -> f64
Total delta budget (immutable).
Sourcepub fn epsilon_remaining(&self) -> f64
pub fn epsilon_remaining(&self) -> f64
Epsilon remaining (may be slightly negative under concurrent load).
Sourcepub fn delta_remaining(&self) -> f64
pub fn delta_remaining(&self) -> f64
Delta remaining.
Sourcepub fn is_exhausted(&self) -> bool
pub fn is_exhausted(&self) -> bool
Returns true if either epsilon or delta remaining is ≤ 0.
Sourcepub fn consume(&self, epsilon: f64, delta: f64) -> Result<(), BudgetError>
pub fn consume(&self, epsilon: f64, delta: f64) -> Result<(), BudgetError>
Atomically consume epsilon and delta from the budget.
Uses a spin-loop via fetch_update to ensure the combined check-then-add
is linearisable. Both values are consumed together or not at all (epsilon
is rolled back on delta failure).
§Errors
BudgetError::BudgetExceededifepsilonwould push spent past the total.BudgetError::DeltaExceededifdeltawould push spent past the total.
Sourcepub fn increment_round(&self) -> u64
pub fn increment_round(&self) -> u64
Increment the round counter and return the new count.
Sourcepub fn snapshot(&self) -> BudgetSnapshot
pub fn snapshot(&self) -> BudgetSnapshot
Take an immutable snapshot of the current budget state.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for PrivacyBudget
impl RefUnwindSafe for PrivacyBudget
impl Send for PrivacyBudget
impl Sync for PrivacyBudget
impl Unpin for PrivacyBudget
impl UnsafeUnpin for PrivacyBudget
impl UnwindSafe for PrivacyBudget
Blanket Implementations§
impl<T> Allocation for T
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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