pub struct AtomicValue<T> { /* private fields */ }Expand description
A concurrent-safe single value container using arc-swap.
This container provides atomic load and store operations for a single value. It’s ideal for configuration values or shared state that needs to be read frequently and updated occasionally.
§Type Parameters
T- The value type
§Example
use litellm_rs::utils::sync::AtomicValue;
let value: AtomicValue<String> = AtomicValue::new("initial".to_string());
assert_eq!(value.load().as_ref(), "initial");
value.store("updated".to_string());
assert_eq!(value.load().as_ref(), "updated");Implementations§
Source§impl<T> AtomicValue<T>
impl<T> AtomicValue<T>
Sourcepub fn load(&self) -> Arc<T> ⓘ
pub fn load(&self) -> Arc<T> ⓘ
Loads the current value.
Returns an Arc pointing to the current value. This is a very fast
operation and does not block other readers or writers.
§Returns
An Arc<T> containing the current value.
§Example
use litellm_rs::utils::sync::AtomicValue;
let value: AtomicValue<i32> = AtomicValue::new(42);
let current = value.load();
assert_eq!(*current, 42);Sourcepub fn swap(&self, value: T) -> Arc<T> ⓘ
pub fn swap(&self, value: T) -> Arc<T> ⓘ
Swaps the current value with a new one, returning the old value.
§Arguments
value- The new value to store
§Returns
An Arc<T> containing the previous value.
§Example
use litellm_rs::utils::sync::AtomicValue;
let value: AtomicValue<i32> = AtomicValue::new(42);
let old = value.swap(100);
assert_eq!(*old, 42);
assert_eq!(*value.load(), 100);Sourcepub fn store_arc(&self, value: Arc<T>)
pub fn store_arc(&self, value: Arc<T>)
Stores a new value from an Arc.
This is useful when you already have an Arc and want to avoid an extra allocation.
§Arguments
value- An Arc containing the new value
§Example
use litellm_rs::utils::sync::AtomicValue;
use std::sync::Arc;
let value: AtomicValue<i32> = AtomicValue::new(42);
value.store_arc(Arc::new(100));
assert_eq!(*value.load(), 100);Sourcepub fn swap_arc(&self, value: Arc<T>) -> Arc<T> ⓘ
pub fn swap_arc(&self, value: Arc<T>) -> Arc<T> ⓘ
Swaps the current value with a new Arc, returning the old value.
§Arguments
value- An Arc containing the new value
§Returns
An Arc<T> containing the previous value.
§Example
use litellm_rs::utils::sync::AtomicValue;
use std::sync::Arc;
let value: AtomicValue<i32> = AtomicValue::new(42);
let old = value.swap_arc(Arc::new(100));
assert_eq!(*old, 42);Source§impl<T: Clone> AtomicValue<T>
impl<T: Clone> AtomicValue<T>
Sourcepub fn update<F>(&self, f: F) -> T
pub fn update<F>(&self, f: F) -> T
Updates the value using a closure, with atomic compare-and-swap retry.
This operation uses ArcSwap::rcu() to perform a read-copy-update loop,
retrying until the compare-and-swap succeeds. The closure may be called
more than once if concurrent updates occur, so it must be side-effect-free
(i.e., Fn rather than FnOnce).
§Arguments
f- A closure that takes a reference to the current value and returns the new value
§Returns
The new value after the update.
§Example
use litellm_rs::utils::sync::AtomicValue;
let value: AtomicValue<i32> = AtomicValue::new(42);
let new_value = value.update(|v| v + 1);
assert_eq!(new_value, 43);Sourcepub fn get(&self) -> T
pub fn get(&self) -> T
Gets a clone of the current value.
This is a convenience method that loads the value and clones it.
§Returns
A clone of the current value.
§Example
use litellm_rs::utils::sync::AtomicValue;
let value: AtomicValue<String> = AtomicValue::new("hello".to_string());
let cloned: String = value.get();
assert_eq!(cloned, "hello");Trait Implementations§
Source§impl<T> Clone for AtomicValue<T>
impl<T> Clone for AtomicValue<T>
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Creates a new AtomicValue pointing to the same underlying data.
Note: This creates a new AtomicValue that shares the same Arc,
but updates to one will not affect the other after the clone.
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for AtomicValue<T>
impl<T: Debug> Debug for AtomicValue<T>
Source§impl<T: Default> Default for AtomicValue<T>
impl<T: Default> Default for AtomicValue<T>
Source§impl<T> From<Arc<T>> for AtomicValue<T>
impl<T> From<Arc<T>> for AtomicValue<T>
Auto Trait Implementations§
impl<T> !Freeze for AtomicValue<T>
impl<T> RefUnwindSafe for AtomicValue<T>
impl<T> Send for AtomicValue<T>
impl<T> Sync for AtomicValue<T>
impl<T> Unpin for AtomicValue<T>
impl<T> UnsafeUnpin for AtomicValue<T>
impl<T> UnwindSafe for AtomicValue<T>
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> BudgetAwareRouting for T
impl<T> BudgetAwareRouting for T
Source§fn filter_by_budget(
&self,
providers: Vec<String>,
budget_router: &BudgetAwareRouter,
) -> Vec<String>
fn filter_by_budget( &self, providers: Vec<String>, budget_router: &BudgetAwareRouter, ) -> Vec<String>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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