autocommit 2022-01-06 19:08:32 CET
This commit is contained in:
parent
cbbc41a463
commit
4a939477a2
51 changed files with 3438 additions and 207 deletions
41
src/utils/asyncevent.rs
Normal file
41
src/utils/asyncevent.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use crate::NumCell;
|
||||
use std::cell::Cell;
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll, Waker};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct AsyncEvent {
|
||||
triggers: NumCell<u32>,
|
||||
waker: Cell<Option<Waker>>,
|
||||
}
|
||||
|
||||
impl AsyncEvent {
|
||||
pub fn trigger(&self) {
|
||||
self.triggers.fetch_add(1);
|
||||
if let Some(waker) = self.waker.take() {
|
||||
waker.wake();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn triggered(&self) -> AsyncEventTriggered {
|
||||
AsyncEventTriggered { ae: self }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AsyncEventTriggered<'a> {
|
||||
ae: &'a AsyncEvent,
|
||||
}
|
||||
|
||||
impl<'a> Future for AsyncEventTriggered<'a> {
|
||||
type Output = ();
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
if self.ae.triggers.replace(0) == 0 {
|
||||
self.ae.waker.set(Some(cx.waker().clone()));
|
||||
Poll::Pending
|
||||
} else {
|
||||
Poll::Ready(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ use crate::utils::buffd::buf_out::{BufFdOut, MsgFds};
|
|||
use std::mem;
|
||||
use std::mem::MaybeUninit;
|
||||
use uapi::OwnedFd;
|
||||
use crate::fixed::Fixed;
|
||||
|
||||
pub struct MsgFormatter<'a> {
|
||||
buf: &'a mut BufFdOut,
|
||||
|
|
@ -29,17 +30,17 @@ impl<'a> MsgFormatter<'a> {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn fixed(&mut self, fixed: f64) -> &mut Self {
|
||||
let int = (fixed * 256.0) as i32;
|
||||
self.buf.write(uapi::as_maybe_uninit_bytes(&int));
|
||||
pub fn fixed(&mut self, fixed: Fixed) -> &mut Self {
|
||||
self.buf.write(uapi::as_maybe_uninit_bytes(&fixed));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn string(&mut self, s: &str) -> &mut Self {
|
||||
pub fn string<S: AsRef<[u8]> + ?Sized>(&mut self, s: &S) -> &mut Self {
|
||||
let s = s.as_ref();
|
||||
let len = s.len() + 1;
|
||||
let cap = (len + 3) & !3;
|
||||
self.uint(len as u32);
|
||||
self.buf.write(uapi::as_maybe_uninit_bytes(s.as_bytes()));
|
||||
self.buf.write(uapi::as_maybe_uninit_bytes(s));
|
||||
let none = [MaybeUninit::new(0); 4];
|
||||
self.buf.write(&none[..cap - len + 1]);
|
||||
self
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
use crate::globals::GlobalName;
|
||||
use crate::object::ObjectId;
|
||||
use crate::utils::buffd::BufFdIn;
|
||||
use bstr::{BStr, ByteSlice};
|
||||
use thiserror::Error;
|
||||
use uapi::OwnedFd;
|
||||
use crate::fixed::Fixed;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum MsgParserError {
|
||||
#[error("The message ended unexpectedly")]
|
||||
UnexpectedEof,
|
||||
#[error("The message contained a non-utf8 string")]
|
||||
NonUtf8,
|
||||
#[error("The message contained a string of size 0")]
|
||||
EmptyString,
|
||||
#[error("Message is missing a required file descriptor")]
|
||||
|
|
@ -57,11 +57,11 @@ impl<'a, 'b> MsgParser<'a, 'b> {
|
|||
self.int().map(|i| GlobalName::from_raw(i as u32))
|
||||
}
|
||||
|
||||
pub fn fixed(&mut self) -> Result<f64, MsgParserError> {
|
||||
self.int().map(|i| i as f64 / 256.0)
|
||||
pub fn fixed(&mut self) -> Result<Fixed, MsgParserError> {
|
||||
self.int().map(|i| Fixed(i))
|
||||
}
|
||||
|
||||
pub fn string(&mut self) -> Result<&'b str, MsgParserError> {
|
||||
pub fn string(&mut self) -> Result<&'b BStr, MsgParserError> {
|
||||
let len = self.uint()? as usize;
|
||||
if len == 0 {
|
||||
return Err(MsgParserError::EmptyString);
|
||||
|
|
@ -71,10 +71,7 @@ impl<'a, 'b> MsgParser<'a, 'b> {
|
|||
return Err(MsgParserError::UnexpectedEof);
|
||||
}
|
||||
let s = &self.data[self.pos..self.pos + len - 1];
|
||||
let s = match std::str::from_utf8(s) {
|
||||
Ok(s) => s,
|
||||
_ => return Err(MsgParserError::NonUtf8),
|
||||
};
|
||||
let s = s.as_bstr();
|
||||
self.pos += cap;
|
||||
Ok(s)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod asyncevent;
|
||||
pub mod buffd;
|
||||
pub mod copyhashmap;
|
||||
pub mod linkedlist;
|
||||
|
|
|
|||
|
|
@ -11,6 +11,10 @@ impl<T> NumCell<T> {
|
|||
Self { t: Cell::new(t) }
|
||||
}
|
||||
|
||||
pub fn replace(&self, n: T) -> T {
|
||||
self.t.replace(n)
|
||||
}
|
||||
|
||||
pub fn load(&self) -> T
|
||||
where
|
||||
T: Copy,
|
||||
|
|
|
|||
|
|
@ -1,24 +1,24 @@
|
|||
pub trait PtrExt<T> {
|
||||
pub trait PtrExt<T: ?Sized> {
|
||||
unsafe fn deref<'a>(self) -> &'a T;
|
||||
}
|
||||
|
||||
pub trait MutPtrExt<T> {
|
||||
pub trait MutPtrExt<T: ?Sized> {
|
||||
unsafe fn deref_mut<'a>(self) -> &'a mut T;
|
||||
}
|
||||
|
||||
impl<T> PtrExt<T> for *const T {
|
||||
impl<T: ?Sized> PtrExt<T> for *const T {
|
||||
unsafe fn deref<'a>(self) -> &'a T {
|
||||
&*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> PtrExt<T> for *mut T {
|
||||
impl<T: ?Sized> PtrExt<T> for *mut T {
|
||||
unsafe fn deref<'a>(self) -> &'a T {
|
||||
&*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> MutPtrExt<T> for *mut T {
|
||||
impl<T: ?Sized> MutPtrExt<T> for *mut T {
|
||||
unsafe fn deref_mut<'a>(self) -> &'a mut T {
|
||||
&mut *self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
use std::cell::RefCell;
|
||||
use std::collections::VecDeque;
|
||||
use std::future::Future;
|
||||
use std::mem;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll, Waker};
|
||||
|
||||
|
|
@ -35,6 +36,10 @@ impl<T> AsyncQueue<T> {
|
|||
pub fn size(&self) -> usize {
|
||||
self.data.borrow().len()
|
||||
}
|
||||
|
||||
pub fn clear(&self) {
|
||||
mem::take(&mut *self.data.borrow_mut());
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AsyncQueuePop<'a, T> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue