From 6203d54ce3230eb3325c67b6ba10f6ccc1a732c6 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 27 Oct 2023 00:16:26 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20zv:=20Avoid=20allocation?= =?UTF-8?q?=20for=20FD=20list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make use of generics and `AsFd` trait to avoid unnecessary allocation of FD list. --- zvariant/src/dbus/de.rs | 75 +++++++++++++++------------ zvariant/src/de.rs | 45 +++++++++------- zvariant/src/gvariant/de.rs | 81 ++++++++++++++++------------- zvariant/src/serialized/data.rs | 92 ++++++++++++++++++++------------- 4 files changed, 169 insertions(+), 124 deletions(-) diff --git a/zvariant/src/dbus/de.rs b/zvariant/src/dbus/de.rs index 26fc98005..49facae89 100644 --- a/zvariant/src/dbus/de.rs +++ b/zvariant/src/dbus/de.rs @@ -4,7 +4,7 @@ use static_assertions::assert_impl_all; use std::{marker::PhantomData, str}; #[cfg(unix)] -use std::os::unix::io::RawFd; +use std::os::fd::AsFd; use crate::{ de::{DeserializerCommon, ValueParseStage}, @@ -18,11 +18,13 @@ use crate::Fd; /// Our D-Bus deserialization implementation. #[derive(Debug)] -pub(crate) struct Deserializer<'de, 'sig, 'f, B>(pub(crate) DeserializerCommon<'de, 'sig, 'f, B>); +pub(crate) struct Deserializer<'de, 'sig, 'f, B, F>( + pub(crate) DeserializerCommon<'de, 'sig, 'f, B, F>, +); -assert_impl_all!(Deserializer<'_, '_, '_, i32>: Send, Sync, Unpin); +assert_impl_all!(Deserializer<'_, '_, '_, i32, ()>: Send, Sync, Unpin); -impl<'de, 'sig, 'f, B> Deserializer<'de, 'sig, 'f, B> +impl<'de, 'sig, 'f, B, F> Deserializer<'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { @@ -31,7 +33,7 @@ where /// On Windows, there is no `fds` argument. pub fn new<'r: 'de, S>( bytes: &'r [u8], - #[cfg(unix)] fds: Option<&'f [RawFd]>, + #[cfg(unix)] fds: Option<&'f [F]>, signature: S, ctxt: EncodingContext, ) -> Result @@ -86,7 +88,8 @@ macro_rules! deserialize_as { } } -impl<'de, 'd, 'sig, 'f, B> de::Deserializer<'de> for &'d mut Deserializer<'de, 'sig, 'f, B> +impl<'de, 'd, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> de::Deserializer<'de> + for &'d mut Deserializer<'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { @@ -367,7 +370,7 @@ where let v = visitor.visit_enum(crate::de::Enum { de: &mut *self, name, - phantom: PhantomData, + _phantoms: (PhantomData, PhantomData), })?; if non_unit { @@ -394,8 +397,8 @@ where } } -struct ArrayDeserializer<'d, 'de, 'sig, 'f, B> { - de: &'d mut Deserializer<'de, 'sig, 'f, B>, +struct ArrayDeserializer<'d, 'de, 'sig, 'f, B, F> { + de: &'d mut Deserializer<'de, 'sig, 'f, B, F>, len: usize, start: usize, // alignment of element @@ -404,11 +407,12 @@ struct ArrayDeserializer<'d, 'de, 'sig, 'f, B> { element_signature_len: usize, } -impl<'d, 'de, 'sig, 'f, B> ArrayDeserializer<'d, 'de, 'sig, 'f, B> +impl<'d, 'de, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> + ArrayDeserializer<'d, 'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { - fn new(de: &'d mut Deserializer<'de, 'sig, 'f, B>) -> Result { + fn new(de: &'d mut Deserializer<'de, 'sig, 'f, B, F>) -> Result { de.0.parse_padding(ARRAY_ALIGNMENT_DBUS)?; de.0.container_depths = de.0.container_depths.inc_array()?; @@ -442,7 +446,7 @@ where { let ctxt = EncodingContext::new_dbus(self.de.0.ctxt.position() + self.de.0.pos); - let mut de = Deserializer::(DeserializerCommon { + let mut de = Deserializer::(DeserializerCommon { ctxt, sig_parser, bytes: subslice(self.de.0.bytes, self.de.0.pos..)?, @@ -493,7 +497,9 @@ where } } -fn deserialize_ay<'de, B>(de: &mut Deserializer<'de, '_, '_, B>) -> Result<&'de [u8]> +fn deserialize_ay<'de, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F>( + de: &mut Deserializer<'de, '_, '_, B, F>, +) -> Result<&'de [u8]> where B: byteorder::ByteOrder, { @@ -508,9 +514,10 @@ where de.0.next_slice(len) } -struct ArraySeqDeserializer<'d, 'de, 'sig, 'f, B>(ArrayDeserializer<'d, 'de, 'sig, 'f, B>); +struct ArraySeqDeserializer<'d, 'de, 'sig, 'f, B, F>(ArrayDeserializer<'d, 'de, 'sig, 'f, B, F>); -impl<'d, 'de, 'sig, 'f, B> SeqAccess<'de> for ArraySeqDeserializer<'d, 'de, 'sig, 'f, B> +impl<'d, 'de, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> SeqAccess<'de> + for ArraySeqDeserializer<'d, 'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { @@ -525,9 +532,10 @@ where } } -struct ArrayMapDeserializer<'d, 'de, 'sig, 'f, B>(ArrayDeserializer<'d, 'de, 'sig, 'f, B>); +struct ArrayMapDeserializer<'d, 'de, 'sig, 'f, B, F>(ArrayDeserializer<'d, 'de, 'sig, 'f, B, F>); -impl<'d, 'de, 'sig, 'f, B> MapAccess<'de> for ArrayMapDeserializer<'d, 'de, 'sig, 'f, B> +impl<'d, 'de, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> MapAccess<'de> + for ArrayMapDeserializer<'d, 'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { @@ -553,11 +561,12 @@ where } #[derive(Debug)] -struct StructureDeserializer<'d, 'de, 'sig, 'f, B> { - de: &'d mut Deserializer<'de, 'sig, 'f, B>, +struct StructureDeserializer<'d, 'de, 'sig, 'f, B, F> { + de: &'d mut Deserializer<'de, 'sig, 'f, B, F>, } -impl<'d, 'de, 'sig, 'f, B> SeqAccess<'de> for StructureDeserializer<'d, 'de, 'sig, 'f, B> +impl<'d, 'de, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> SeqAccess<'de> + for StructureDeserializer<'d, 'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { @@ -579,19 +588,20 @@ where } #[derive(Debug)] -struct ValueDeserializer<'d, 'de, 'sig, 'f, B> { - de: &'d mut Deserializer<'de, 'sig, 'f, B>, +struct ValueDeserializer<'d, 'de, 'sig, 'f, B, F> { + de: &'d mut Deserializer<'de, 'sig, 'f, B, F>, stage: ValueParseStage, sig_start: usize, } -impl<'d, 'de, 'sig, 'f, B> ValueDeserializer<'d, 'de, 'sig, 'f, B> +impl<'d, 'de, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> + ValueDeserializer<'d, 'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { - fn new(de: &'d mut Deserializer<'de, 'sig, 'f, B>) -> Self { + fn new(de: &'d mut Deserializer<'de, 'sig, 'f, B, F>) -> Self { let sig_start = de.0.pos; - ValueDeserializer:: { + ValueDeserializer:: { de, stage: ValueParseStage::Signature, sig_start, @@ -599,7 +609,8 @@ where } } -impl<'d, 'de, 'sig, 'f, B> SeqAccess<'de> for ValueDeserializer<'d, 'de, 'sig, 'f, B> +impl<'d, 'de, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> SeqAccess<'de> + for ValueDeserializer<'d, 'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { @@ -633,7 +644,7 @@ where EncodingFormat::DBus, self.de.0.ctxt.position() + value_start, ); - let mut de = Deserializer::(DeserializerCommon { + let mut de = Deserializer::(DeserializerCommon { ctxt, sig_parser, bytes: subslice(self.de.0.bytes, value_start..)?, @@ -653,12 +664,12 @@ where } } -impl<'de, 'd, 'sig, 'f, B> crate::de::GetDeserializeCommon<'de, 'sig, 'f, B> - for &'d mut Deserializer<'de, 'sig, 'f, B> +impl<'de, 'd, 'sig, 'f, B, F> crate::de::GetDeserializeCommon<'de, 'sig, 'f, B, F> + for &'d mut Deserializer<'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { - fn common_mut<'dr>(self) -> &'dr mut DeserializerCommon<'de, 'sig, 'f, B> + fn common_mut<'dr>(self) -> &'dr mut DeserializerCommon<'de, 'sig, 'f, B, F> where Self: 'dr, { @@ -666,8 +677,8 @@ where } } -impl<'de, 'd, 'sig, 'f, B> EnumAccess<'de> - for crate::de::Enum> +impl<'de, 'd, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> EnumAccess<'de> + for crate::de::Enum, F> where B: byteorder::ByteOrder, { diff --git a/zvariant/src/de.rs b/zvariant/src/de.rs index c18b6cb0e..7e103b36b 100644 --- a/zvariant/src/de.rs +++ b/zvariant/src/de.rs @@ -4,7 +4,7 @@ use static_assertions::assert_impl_all; use std::{marker::PhantomData, str}; #[cfg(unix)] -use std::os::unix::io::RawFd; +use std::os::fd::{AsFd, AsRawFd}; #[cfg(feature = "gvariant")] use crate::gvariant::Deserializer as GVDeserializer; @@ -19,14 +19,14 @@ use crate::Fd; /// Our deserialization implementation. #[derive(Debug)] -pub(crate) struct DeserializerCommon<'de, 'sig, 'f, B> { +pub(crate) struct DeserializerCommon<'de, 'sig, 'f, B, F> { pub(crate) ctxt: EncodingContext, pub(crate) bytes: &'de [u8], #[cfg(unix)] - pub(crate) fds: Option<&'f [RawFd]>, + pub(crate) fds: Option<&'f [F]>, #[cfg(not(unix))] - pub(crate) fds: PhantomData<&'f ()>, + pub(crate) fds: PhantomData<&'f F>, pub(crate) pos: usize, @@ -42,26 +42,30 @@ pub(crate) struct DeserializerCommon<'de, 'sig, 'f, B> { /// Using this deserializer involves an redirection to the actual deserializer. It's best /// to use the serialization functions, e.g [`crate::to_bytes`] or specific serializers, /// [`crate::dbus::Deserializer`] or [`crate::zvariant::Deserializer`]. -pub(crate) enum Deserializer<'ser, 'sig, 'f, B> { - DBus(DBusDeserializer<'ser, 'sig, 'f, B>), +pub(crate) enum Deserializer<'ser, 'sig, 'f, B, F> { + DBus(DBusDeserializer<'ser, 'sig, 'f, B, F>), #[cfg(feature = "gvariant")] - GVariant(GVDeserializer<'ser, 'sig, 'f, B>), + GVariant(GVDeserializer<'ser, 'sig, 'f, B, F>), } -assert_impl_all!(Deserializer<'_, '_, '_, u8>: Send, Sync, Unpin); +assert_impl_all!(Deserializer<'_, '_, '_, u8, ()>: Send, Sync, Unpin); -impl<'de, 'sig, 'f, B> DeserializerCommon<'de, 'sig, 'f, B> +#[cfg(unix)] +impl<'de, 'sig, 'f, B, F> DeserializerCommon<'de, 'sig, 'f, B, F> where - B: byteorder::ByteOrder, + F: AsFd, { - #[cfg(unix)] pub fn get_fd(&self, idx: u32) -> Result { self.fds - .and_then(|fds| fds.get(idx as usize)) - .copied() + .and_then(|fds| fds.get(idx as usize).map(|fd| fd.as_fd().as_raw_fd())) .ok_or(Error::UnknownFd) } +} +impl<'de, 'sig, 'f, B, F> DeserializerCommon<'de, 'sig, 'f, B, F> +where + B: byteorder::ByteOrder, +{ pub fn parse_padding(&mut self, alignment: usize) -> Result { let padding = padding_for_n_bytes(self.abs_pos(), alignment); if padding > 0 { @@ -142,7 +146,8 @@ macro_rules! deserialize_method { } } -impl<'de, 'd, 'sig, 'f, B> de::Deserializer<'de> for &'d mut Deserializer<'de, 'sig, 'f, B> +impl<'de, 'd, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> de::Deserializer<'de> + for &'d mut Deserializer<'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { @@ -232,26 +237,26 @@ where } } -pub(crate) trait GetDeserializeCommon<'de, 'sig, 'f, B> +pub(crate) trait GetDeserializeCommon<'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { - fn common_mut<'d>(self) -> &'d mut DeserializerCommon<'de, 'sig, 'f, B> + fn common_mut<'d>(self) -> &'d mut DeserializerCommon<'de, 'sig, 'f, B, F> where Self: 'd; } // Enum handling is very generic so it can be here and specific deserializers can use this. -pub(crate) struct Enum { +pub(crate) struct Enum { pub(crate) de: D, pub(crate) name: &'static str, - pub(crate) phantom: PhantomData, + pub(crate) _phantoms: (PhantomData, PhantomData), } -impl<'de, 'sig, 'f, B, D> VariantAccess<'de> for Enum +impl<'de, 'sig, 'f, B, D, F> VariantAccess<'de> for Enum where B: byteorder::ByteOrder, - D: de::Deserializer<'de, Error = Error> + GetDeserializeCommon<'de, 'sig, 'f, B>, + D: de::Deserializer<'de, Error = Error> + GetDeserializeCommon<'de, 'sig, 'f, B, F>, { type Error = Error; diff --git a/zvariant/src/gvariant/de.rs b/zvariant/src/gvariant/de.rs index 0210d3b07..a3e4cee2b 100644 --- a/zvariant/src/gvariant/de.rs +++ b/zvariant/src/gvariant/de.rs @@ -4,7 +4,7 @@ use static_assertions::assert_impl_all; use std::{ffi::CStr, marker::PhantomData, str}; #[cfg(unix)] -use std::os::unix::io::RawFd; +use std::os::fd::AsFd; use crate::{ de::{DeserializerCommon, ValueParseStage}, @@ -17,11 +17,11 @@ use crate::{ /// Our GVariant deserialization implementation. #[derive(Debug)] -pub struct Deserializer<'de, 'sig, 'f, B>(pub(crate) DeserializerCommon<'de, 'sig, 'f, B>); +pub struct Deserializer<'de, 'sig, 'f, B, F>(pub(crate) DeserializerCommon<'de, 'sig, 'f, B, F>); -assert_impl_all!(Deserializer<'_, '_,'_, i32>: Send, Sync, Unpin); +assert_impl_all!(Deserializer<'_, '_,'_, i32, ()>: Send, Sync, Unpin); -impl<'de, 'sig, 'f, B> Deserializer<'de, 'sig, 'f, B> +impl<'de, 'sig, 'f, B, F> Deserializer<'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { @@ -30,7 +30,7 @@ where /// On Windows, the function doesn't have `fds` argument. pub fn new<'r: 'de, S>( bytes: &'r [u8], - #[cfg(unix)] fds: Option<&'f [RawFd]>, + #[cfg(unix)] fds: Option<&'f [F]>, signature: S, ctxt: EncodingContext, ) -> Result @@ -65,7 +65,7 @@ macro_rules! deserialize_basic { { let ctxt = EncodingContext::new_dbus(self.0.ctxt.position() + self.0.pos); - let mut dbus_de = crate::dbus::Deserializer::(DeserializerCommon:: { + let mut dbus_de = crate::dbus::Deserializer::(DeserializerCommon:: { ctxt, sig_parser: self.0.sig_parser.clone(), bytes: subslice(self.0.bytes, self.0.pos..)?, @@ -100,7 +100,8 @@ macro_rules! deserialize_as { } } -impl<'de, 'd, 'sig, 'f, B> de::Deserializer<'de> for &'d mut Deserializer<'de, 'sig, 'f, B> +impl<'de, 'd, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> de::Deserializer<'de> + for &'d mut Deserializer<'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { @@ -225,7 +226,7 @@ where self.0.bytes.len() - 1 }; - let mut de = Deserializer::(DeserializerCommon { + let mut de = Deserializer::(DeserializerCommon { ctxt, sig_parser: self.0.sig_parser.clone(), bytes: subslice(self.0.bytes, self.0.pos..end)?, @@ -381,7 +382,7 @@ where let v = visitor.visit_enum(crate::de::Enum { de: &mut *self, name, - phantom: PhantomData, + _phantoms: (PhantomData, PhantomData), })?; if non_unit { @@ -397,7 +398,9 @@ where } } -fn deserialize_ay<'de, B>(de: &mut Deserializer<'de, '_, '_, B>) -> Result<&'de [u8]> +fn deserialize_ay<'de, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F>( + de: &mut Deserializer<'de, '_, '_, B, F>, +) -> Result<&'de [u8]> where B: byteorder::ByteOrder, { @@ -411,8 +414,8 @@ where de.0.next_slice(len) } -struct ArrayDeserializer<'d, 'de, 'sig, 'f, B> { - de: &'d mut Deserializer<'de, 'sig, 'f, B>, +struct ArrayDeserializer<'d, 'de, 'sig, 'f, B, F> { + de: &'d mut Deserializer<'de, 'sig, 'f, B, F>, len: usize, start: usize, // alignment of element @@ -427,11 +430,12 @@ struct ArrayDeserializer<'d, 'de, 'sig, 'f, B> { key_offset_size: Option, } -impl<'d, 'de, 'sig, 'f, B> ArrayDeserializer<'d, 'de, 'sig, 'f, B> +impl<'d, 'de, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> + ArrayDeserializer<'d, 'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { - fn new(de: &'d mut Deserializer<'de, 'sig, 'f, B>) -> Result { + fn new(de: &'d mut Deserializer<'de, 'sig, 'f, B, F>) -> Result { de.0.container_depths = de.0.container_depths.inc_array()?; let mut len = de.0.bytes.len() - de.0.pos; @@ -513,7 +517,8 @@ where } } -impl<'d, 'de, 'sig, 'f, B> SeqAccess<'de> for ArrayDeserializer<'d, 'de, 'sig, 'f, B> +impl<'d, 'de, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> SeqAccess<'de> + for ArrayDeserializer<'d, 'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { @@ -540,7 +545,7 @@ where ); let end = self.element_end(true)?; - let mut de = Deserializer::(DeserializerCommon { + let mut de = Deserializer::(DeserializerCommon { ctxt, sig_parser: self.de.0.sig_parser.clone(), bytes: subslice(self.de.0.bytes, self.de.0.pos..end)?, @@ -565,7 +570,8 @@ where } } -impl<'d, 'de, 'sig, 'f, B> MapAccess<'de> for ArrayDeserializer<'d, 'de, 'sig, 'f, B> +impl<'d, 'de, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> MapAccess<'de> + for ArrayDeserializer<'d, 'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { @@ -607,7 +613,7 @@ where None => element_end, }; - let mut de = Deserializer::(DeserializerCommon { + let mut de = Deserializer::(DeserializerCommon { ctxt, sig_parser: self.de.0.sig_parser.clone(), bytes: subslice(self.de.0.bytes, self.de.0.pos..key_end)?, @@ -647,7 +653,7 @@ where // Skip key signature (always 1 char) sig_parser.skip_char()?; - let mut de = Deserializer::(DeserializerCommon { + let mut de = Deserializer::(DeserializerCommon { ctxt, sig_parser, bytes: subslice(self.de.0.bytes, self.de.0.pos..value_end)?, @@ -676,8 +682,8 @@ where } #[derive(Debug)] -struct StructureDeserializer<'d, 'de, 'sig, 'f, B> { - de: &'d mut Deserializer<'de, 'sig, 'f, B>, +struct StructureDeserializer<'d, 'de, 'sig, 'f, B, F> { + de: &'d mut Deserializer<'de, 'sig, 'f, B, F>, start: usize, end: usize, // Length of all the offsets after the array @@ -686,7 +692,8 @@ struct StructureDeserializer<'d, 'de, 'sig, 'f, B> { offset_size: FramingOffsetSize, } -impl<'d, 'de, 'sig, 'f, B> SeqAccess<'de> for StructureDeserializer<'d, 'de, 'sig, 'f, B> +impl<'d, 'de, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> SeqAccess<'de> + for StructureDeserializer<'d, 'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { @@ -732,7 +739,7 @@ where }; let sig_parser = self.de.0.sig_parser.clone(); - let mut de = Deserializer::(DeserializerCommon { + let mut de = Deserializer::(DeserializerCommon { ctxt, sig_parser, bytes: subslice(self.de.0.bytes, self.de.0.pos..element_end)?, @@ -760,8 +767,8 @@ where } #[derive(Debug)] -struct ValueDeserializer<'d, 'de, 'sig, 'f, B> { - de: &'d mut Deserializer<'de, 'sig, 'f, B>, +struct ValueDeserializer<'d, 'de, 'sig, 'f, B, F> { + de: &'d mut Deserializer<'de, 'sig, 'f, B, F>, stage: ValueParseStage, sig_start: usize, sig_end: usize, @@ -769,11 +776,12 @@ struct ValueDeserializer<'d, 'de, 'sig, 'f, B> { value_end: usize, } -impl<'d, 'de, 'sig, 'f, B> ValueDeserializer<'d, 'de, 'sig, 'f, B> +impl<'d, 'de, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> + ValueDeserializer<'d, 'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { - fn new(de: &'d mut Deserializer<'de, 'sig, 'f, B>) -> Result { + fn new(de: &'d mut Deserializer<'de, 'sig, 'f, B, F>) -> Result { // GVariant format has signature at the end let mut separator_pos = None; @@ -803,7 +811,7 @@ where Some(separator_pos) => (separator_pos + 1, de.0.bytes.len(), de.0.pos, separator_pos), }; - Ok(ValueDeserializer:: { + Ok(ValueDeserializer:: { de, stage: ValueParseStage::Signature, sig_start, @@ -814,7 +822,8 @@ where } } -impl<'d, 'de, 'sig, 'f, B> SeqAccess<'de> for ValueDeserializer<'d, 'de, 'sig, 'f, B> +impl<'d, 'de, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> SeqAccess<'de> + for ValueDeserializer<'d, 'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { @@ -831,7 +840,7 @@ where let signature = Signature::from_static_str_unchecked(VARIANT_SIGNATURE_STR); let sig_parser = SignatureParser::new(signature); - let mut de = Deserializer::(DeserializerCommon { + let mut de = Deserializer::(DeserializerCommon { // No padding in signatures so just pass the same context ctxt: self.de.0.ctxt, sig_parser, @@ -856,7 +865,7 @@ where self.de.0.ctxt.format(), self.de.0.ctxt.position() + self.value_start, ); - let mut de = Deserializer::(DeserializerCommon { + let mut de = Deserializer::(DeserializerCommon { ctxt, sig_parser, bytes: subslice(self.de.0.bytes, self.value_start..self.value_end)?, @@ -877,12 +886,12 @@ where } } -impl<'de, 'd, 'sig, 'f, B> crate::de::GetDeserializeCommon<'de, 'sig, 'f, B> - for &'d mut Deserializer<'de, 'sig, 'f, B> +impl<'de, 'd, 'sig, 'f, B, F> crate::de::GetDeserializeCommon<'de, 'sig, 'f, B, F> + for &'d mut Deserializer<'de, 'sig, 'f, B, F> where B: byteorder::ByteOrder, { - fn common_mut<'dr>(self) -> &'dr mut DeserializerCommon<'de, 'sig, 'f, B> + fn common_mut<'dr>(self) -> &'dr mut DeserializerCommon<'de, 'sig, 'f, B, F> where Self: 'dr, { @@ -890,8 +899,8 @@ where } } -impl<'de, 'd, 'sig, 'f, B> EnumAccess<'de> - for crate::de::Enum> +impl<'de, 'd, 'sig, 'f, B, #[cfg(unix)] F: AsFd, #[cfg(not(unix))] F> EnumAccess<'de> + for crate::de::Enum, F> where B: byteorder::ByteOrder, { diff --git a/zvariant/src/serialized/data.rs b/zvariant/src/serialized/data.rs index 06528c4fa..ba81076ae 100644 --- a/zvariant/src/serialized/data.rs +++ b/zvariant/src/serialized/data.rs @@ -217,29 +217,39 @@ impl<'bytes, 'fds, B: ByteOrder> Data<'bytes, 'fds, B> { let signature = signature.try_into().map_err(Into::into)?; #[cfg(unix)] - let fds = self - .inner - .fds - .iter() - .map(std::os::fd::AsRawFd::as_raw_fd) - .collect::>(); + let fds = &self.inner.fds; let mut de = match self.context.format() { #[cfg(feature = "gvariant")] - EncodingFormat::GVariant => crate::gvariant::Deserializer::new( - self.bytes(), + EncodingFormat::GVariant => { #[cfg(unix)] - Some(&fds), - signature, - self.context, - ) + { + crate::gvariant::Deserializer::new( + self.bytes(), + Some(fds), + signature, + self.context, + ) + } + #[cfg(not(unix))] + { + crate::gvariant::Deserializer::<_, ()>::new( + self.bytes(), + signature, + self.context, + ) + } + } .map(Deserializer::GVariant)?, - EncodingFormat::DBus => crate::dbus::Deserializer::new( - self.bytes(), + EncodingFormat::DBus => { #[cfg(unix)] - Some(&fds), - signature, - self.context, - ) + { + crate::dbus::Deserializer::new(self.bytes(), Some(fds), signature, self.context) + } + #[cfg(not(unix))] + { + crate::dbus::Deserializer::<_, ()>::new(self.bytes(), signature, self.context) + } + } .map(Deserializer::DBus)?, }; @@ -280,29 +290,39 @@ impl<'bytes, 'fds, B: ByteOrder> Data<'bytes, 'fds, B> { let signature = S::dynamic_signature(&seed).to_owned(); #[cfg(unix)] - let fds = self - .inner - .fds - .iter() - .map(std::os::fd::AsRawFd::as_raw_fd) - .collect::>(); + let fds = &self.inner.fds; let mut de = match self.context.format() { #[cfg(feature = "gvariant")] - EncodingFormat::GVariant => crate::gvariant::Deserializer::new( - self.bytes(), + EncodingFormat::GVariant => { #[cfg(unix)] - Some(&fds), - signature, - self.context, - ) + { + crate::gvariant::Deserializer::new( + self.bytes(), + Some(fds), + signature, + self.context, + ) + } + #[cfg(not(unix))] + { + crate::gvariant::Deserializer::<_, ()>::new( + self.bytes(), + signature, + self.context, + ) + } + } .map(Deserializer::GVariant)?, - EncodingFormat::DBus => crate::dbus::Deserializer::new( - self.bytes(), + EncodingFormat::DBus => { #[cfg(unix)] - Some(&fds), - signature, - self.context, - ) + { + crate::dbus::Deserializer::new(self.bytes(), Some(fds), signature, self.context) + } + #[cfg(not(unix))] + { + crate::dbus::Deserializer::<_, ()>::new(self.bytes(), signature, self.context) + } + } .map(Deserializer::DBus)?, };