1
0
Fork 0
forked from wry/wry

autocommit 2022-01-09 14:36:34 CET

This commit is contained in:
Julian Orth 2022-01-09 14:36:34 +01:00
parent 02d1c90501
commit 928f94daa6
14 changed files with 200 additions and 102 deletions

View file

@ -70,7 +70,7 @@ impl<'a> MsgFormatter<'a> {
fds: self.fds,
};
f(&mut fmt);
let len = self.buf.out_pos - pos + 4;
let len = self.buf.out_pos - pos - 4;
let none = [MaybeUninit::new(0); 4];
self.buf.write(&none[..self.buf.out_pos.wrapping_neg() & 3]);
len as u32

49
src/utils/clonecell.rs Normal file
View file

@ -0,0 +1,49 @@
use std::cell::UnsafeCell;
use std::mem;
use std::rc::Rc;
use crate::utils::ptr_ext::{MutPtrExt, PtrExt};
pub struct CloneCell<T: UnsafeCellCloneSafe> {
data: UnsafeCell<T>,
}
impl<T: UnsafeCellCloneSafe> CloneCell<T> {
pub fn new(t: T) -> Self {
Self {
data: UnsafeCell::new(t),
}
}
#[inline(always)]
pub fn get(&self) -> T {
unsafe {
self.data.get().deref().clone()
}
}
#[inline(always)]
pub fn set(&self, t: T) {
unsafe {
let _ = mem::replace(self.data.get().deref_mut(), t);
}
}
#[inline(always)]
pub fn take(&self) -> T where T: Default {
unsafe {
mem::take(self.data.get().deref_mut())
}
}
}
impl<T: Default + UnsafeCellCloneSafe> Default for CloneCell<T> {
fn default() -> Self {
Self::new(Default::default())
}
}
pub unsafe trait UnsafeCellCloneSafe: Clone { }
unsafe impl<T: UnsafeCellCloneSafe> UnsafeCellCloneSafe for Option<T> { }
unsafe impl<T: ?Sized> UnsafeCellCloneSafe for Rc<T> { }

View file

@ -8,3 +8,4 @@ pub mod oneshot;
pub mod ptr_ext;
pub mod queue;
pub mod vec_ext;
pub mod clonecell;