use std::sync::{Arc, OnceLock};
use crate::rt::shard::Shared;
use crate::sync::internal::{AtomicBool, Ordering};
pub(crate) struct Target {
first: Arc<Shared>,
then: OnceLock<Arc<Shared>>,
moved: AtomicBool,
}
impl Target {
pub(crate) fn new(first: Arc<Shared>) -> Self {
Self {
first,
then: OnceLock::new(),
moved: AtomicBool::new(false),
}
}
pub(crate) fn get(&self) -> &Arc<Shared> {
if self.moved.load(Ordering::SeqCst) {
self.then.get().unwrap_or(&self.first)
} else {
&self.first
}
}
pub(crate) fn move_to(&self, to: &Arc<Shared>) {
if self.then.set(Arc::clone(to)).is_ok() {
self.moved.store(true, Ordering::SeqCst);
}
}
}