Serdes fixes and general improvements

This commit is contained in:
NGnius (Graham) 2025-02-12 18:44:33 -05:00
parent 8cb4425c83
commit eb7274c599
5 changed files with 62 additions and 3 deletions

View file

@ -5,3 +5,4 @@ edition = "2021"
[dependencies]
polariton = { version = "0.1.0", path = "../" }
log = "0.4"

View file

@ -46,7 +46,7 @@ impl <U: Send + Sync> OperationsHandler<U> {
}
pub fn with_state<S: Send + Sync + 'static, O: Operation<State=S, User=U> + OperationCode + 'static>(mut self, op: O, state: S) -> Self {
self.op_map.insert(O::op_code(), Box::new(SyncedOpWrapper {
self.insert_op(O::op_code(), Box::new(SyncedOpWrapper {
state: Mutex::new(state),
handler: op,
}));
@ -54,12 +54,18 @@ impl <U: Send + Sync> OperationsHandler<U> {
}
pub fn without_state<O: Operation<State=(), User=U> + OperationCode + 'static>(mut self, op: O) -> Self {
self.op_map.insert(O::op_code(), Box::new(StatelessOpWrapper {
self.insert_op(O::op_code(), Box::new(StatelessOpWrapper {
handler: op,
}));
self
}
fn insert_op(&mut self, code: u8, handler: Box<dyn WrappedOperation<U>>) {
if self.op_map.insert(code, handler).is_some() {
log::warn!("Replacing operation for code {}", code);
}
}
fn try_pass_to_handler(&self, code: u8, params: ParameterTable, user:&U) -> OperationResponse {
if let Some(handler) = self.op_map.get(&code) {
handler.handle(params, user)

View file

@ -24,3 +24,6 @@ pub use type_prefixed::Typed;
mod typed_dict;
pub use typed_dict::Dict;
mod typed_arr;
pub use typed_arr::Arr;

View file

@ -41,7 +41,7 @@ pub enum Typed {
#[brw(magic = 120u8)]
Bytes(super::SizePrefixedVec<i32, u8>),
#[brw(magic = 121u8)]
Arr(super::SizePrefixedVec<i16, Typed>),
Arr(super::Arr),
#[brw(magic = 122u8)]
ObjArr(super::SizePrefixedVec<i16, Typed>),
}

View file

@ -0,0 +1,49 @@
use binrw::{BinRead, BinWrite};
use super::Typed;
#[derive(Debug, Clone)]
pub struct Arr {
pub ty: u8,
pub items: Vec<Typed>,
}
impl BinRead for Arr {
type Args<'a> = ();
fn read_options<R: std::io::Read + std::io::Seek>(
reader: &mut R,
endian: binrw::Endian,
args: Self::Args<'_>,
) -> binrw::BinResult<Self> {
let len = i16::read_options(reader, endian, args)?;
let ty = u8::read_options(reader, endian, args)?;
let mut items = Vec::with_capacity(len as _);
for _ in 0..len {
let item = Typed::with_magic(ty, reader)?;
items.push(item);
}
Ok(Self {
ty, items,
})
}
}
impl BinWrite for Arr {
type Args<'a> = ();
fn write_options<W: std::io::Write + std::io::Seek>(
&self,
writer: &mut W,
endian: binrw::Endian,
args: Self::Args<'_>,
) -> binrw::BinResult<()> {
(self.items.len() as i16).write_options(writer, endian, args)?;
self.ty.write_options(writer, endian, args)?;
for item in &self.items {
item.write_inner(writer)?;
}
Ok(())
}
}