1
0
Fork 0
forked from wry/wry

autocommit 2022-01-06 19:08:32 CET

This commit is contained in:
Julian Orth 2022-01-06 19:08:32 +01:00
parent cbbc41a463
commit 4a939477a2
51 changed files with 3438 additions and 207 deletions

View file

@ -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

View file

@ -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)
}