pub struct TokenContainer {
token: std::sync::atomic::AtomicPtr<String>,
start: std::sync::atomic::AtomicU64,
duration: std::sync::atomic::AtomicU64,
renewable: std::sync::atomic::AtomicBool,
}
impl TokenContainer {
pub fn new() -> TokenContainer {
let boxed_empty = Box::new(String::from(""));
let empty_ptr = Box::into_raw(boxed_empty);
TokenContainer {
token: std::sync::atomic::AtomicPtr::new(empty_ptr),
start: std::sync::atomic::AtomicU64::new(0),
duration: std::sync::atomic::AtomicU64::new(0),
renewable: std::sync::atomic::AtomicBool::new(false),
}
}
pub fn get_start(&self) -> u64 {
self.start.load(std::sync::atomic::Ordering::SeqCst)
}
pub fn get_duration(&self) -> u64 {
self.duration.load(std::sync::atomic::Ordering::SeqCst)
}
pub fn get_renewable(&self) -> bool {
self.renewable.load(std::sync::atomic::Ordering::SeqCst)
}
pub fn get_token(&self) -> Option<String> {
let raw_token = self.token.load(std::sync::atomic::Ordering::SeqCst);
match unsafe { raw_token.as_ref() } {
None => None,
Some(s) => Some(s.clone()),
}
}
pub fn set_start(&self, new_start: u64) {
self.start
.store(new_start, std::sync::atomic::Ordering::SeqCst);
}
pub fn set_duration(&self, new_duration: u64) {
self.duration
.store(new_duration, std::sync::atomic::Ordering::SeqCst);
}
pub fn set_renewable(&self, new_renewable: bool) {
self.renewable
.store(new_renewable, std::sync::atomic::Ordering::SeqCst)
}
pub fn set_token(&self, token: String) {
let boxed = Box::new(token);
let new_ptr = Box::into_raw(boxed);
let old_ptr = self
.token
.swap(new_ptr, std::sync::atomic::Ordering::SeqCst);
unsafe {
drop(Box::from_raw(old_ptr));
}
}
}