use std::mem;
pub(in crate::depot) use state::State;
mod state;
pub(in crate::depot) struct Owned<V, S: State<V> = ()> {
value: mem::ManuallyDrop<V>,
state: S,
}
impl<V> Owned<V> {
pub(in crate::depot) fn new(value: V) -> Self {
Self {
value: mem::ManuallyDrop::new(value),
state: (),
}
}
pub(in crate::depot) fn into_value(self) -> V {
self.into_parts().0
}
}
impl<V, S: State<V>> Owned<V, S> {
pub(in crate::depot) unsafe fn from_parts(value: V, state: S) -> Self {
Self {
value: mem::ManuallyDrop::new(value),
state,
}
}
pub(in crate::depot) fn into_parts(self) -> (V, S) {
let mut owner = mem::ManuallyDrop::new(self);
(
unsafe { mem::ManuallyDrop::take(&mut owner.value) },
owner.state,
)
}
}
impl<V, S: State<V>> Drop for Owned<V, S> {
fn drop(&mut self) {
unsafe { self.state.clear(&mut self.value) };
}
}