有时,我发现自己使用!Sync
类型(例如bumpalo::Bump
)作为我想要成为Sync
的类型实现的一部分.现在我用unsafe impl Sync
来表示外部类型,在&mut self
种方法中只涉及内部类型.但是,用下面这样的包装器来封装不安全性是否合理?特别是,Send
边界是否正确?
pub struct UnCell<T> {
value: T
}
unsafe impl<T> Sync for UnCell<T> {}
impl<T> UnCell<T> {
pub fn new(value: T) -> Self {
Self { value }
}
pub fn into_inner(self) -> T {
self.value
}
pub fn get(&mut self) -> &T {
&self.value
}
}
impl<T: Send> UnCell<T> {
pub fn get_mut(&mut self) -> &mut T {
&mut self.value
}
}