Skip to content

Commit

Permalink
variant macro no longer parses the signature twice, Variant has gaine…
Browse files Browse the repository at this point in the history
…d a function to continue unmarshalling with a given signature
  • Loading branch information
Moritz Borcherding committed Mar 5, 2024
1 parent f84fd6c commit 20af8f2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
28 changes: 18 additions & 10 deletions rustbus/src/wire/unmarshal/traits/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::signature;
use crate::wire::errors::UnmarshalError;
use crate::wire::marshal::traits::SignatureBuffer;
use crate::wire::unmarshal;
use crate::wire::unmarshal::UnmarshalResult;
use crate::wire::unmarshal_context::UnmarshalContext;
use crate::Signature;
use crate::Unmarshal;
Expand Down Expand Up @@ -247,6 +248,22 @@ impl<'buf, 'fds> Variant<'fds, 'buf> {
let mut ctx = self.sub_ctx;
T::unmarshal(&mut ctx)
}

pub fn unmarshal_with_sig(
sig: signature::Type,
ctx: &mut UnmarshalContext<'fds, 'buf>,
) -> UnmarshalResult<Self> {
ctx.align_to(sig.get_alignment())?;

let val_bytes =
crate::wire::validate_raw::validate_marshalled(ctx.byteorder, 0, ctx.remainder(), &sig)
.map_err(|e| e.1)?;

Ok(Variant {
sig,
sub_ctx: ctx.sub_context(val_bytes)?,
})
}
}

impl Signature for Variant<'_, '_> {
Expand Down Expand Up @@ -277,16 +294,7 @@ impl<'buf, 'fds> Unmarshal<'buf, 'fds> for Variant<'fds, 'buf> {
}
let sig = sigs.remove(0);

ctx.align_to(sig.get_alignment())?;

let val_bytes =
crate::wire::validate_raw::validate_marshalled(ctx.byteorder, 0, ctx.remainder(), &sig)
.map_err(|e| e.1)?;

Ok(Variant {
sig,
sub_ctx: ctx.sub_context(val_bytes)?,
})
Self::unmarshal_with_sig(sig, ctx)
}
}

Expand Down
8 changes: 0 additions & 8 deletions rustbus/src/wire/unmarshal_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ impl<'fds, 'buf> UnmarshalContext<'fds, 'buf> {
pub fn read_raw(&mut self, length: usize) -> UnmarshalResult<&'buf [u8]> {
self.cursor.read_raw(length)
}

pub fn reset(&mut self, reset_by: usize) {
self.cursor.reset(reset_by)
}
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -205,10 +201,6 @@ impl<'buf> Cursor<'buf> {
Ok(elements)
}

pub fn reset(&mut self, reset_by: usize) {
self.offset -= reset_by;
}

pub fn advance(&mut self, advance_by: usize) {
self.offset += advance_by;
}
Expand Down
12 changes: 8 additions & 4 deletions rustbus/src/wire/variant_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ macro_rules! dbus_variant_var_unmarshal {
ctx: &mut $crate::wire::unmarshal_context::UnmarshalContext<'fds, 'buf>
) -> $crate::wire::unmarshal::UnmarshalResult<Self> {
use $crate::Signature;
use $crate::Unmarshal;
use $crate::wire::marshal::traits::SignatureBuffer;

let sig_str = ctx.read_signature()?;
Expand All @@ -354,9 +353,14 @@ macro_rules! dbus_variant_var_unmarshal {
return Ok(Self::$name(v));
}
)+
// TODO this should just be a function on Variant that takes the signature str
ctx.reset(1 + sig_str.as_bytes().len() + 1);
let var = <$crate::wire::unmarshal::traits::Variant as Unmarshal>::unmarshal(ctx)?;
let Ok(mut sigs) = $crate::signature::Type::parse_description(sig_str) else {
return Err($crate::wire::errors::UnmarshalError::WrongSignature);
};
if sigs.len() != 1 {
return Err($crate::wire::errors::UnmarshalError::WrongSignature);
}
let sig = sigs.remove(0);
let var = $crate::wire::unmarshal::traits::Variant::unmarshal_with_sig(sig, ctx)?;
Ok(Self::Catchall(var))
}
}
Expand Down

0 comments on commit 20af8f2

Please sign in to comment.