Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
zv: Replace Error::Io(..) by Error::InputOutput(Arc<..>)
Browse files Browse the repository at this point in the history
This is so that we can implement `Clone` for `Error` in a following
commit w/o ending up tranforming all I/O errors to generic string
errors during cloning (`std::io::Error` doesn't implement `Clone`[1]).

We still keep `Io` variant around for backwards-compat but we deprecate
it and don't return errors with that variant for any of our API anymore.

[1]: rust-lang/rust#24135
  • Loading branch information
zeenix committed Dec 9, 2022
1 parent acf6e56 commit ae99095
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
45 changes: 32 additions & 13 deletions zvariant/src/dbus/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ macro_rules! serialize_basic {
($method:ident($type:ty) $write_method:ident($as:ty)) => {
fn $method(self, v: $type) -> Result<()> {
self.0.prep_serialize_basic::<$type>()?;
self.0.$write_method::<B>(v as $as).map_err(Error::Io)
self.0.$write_method::<B>(v as $as).map_err(|e| Error::InputOutput(e.into()))
}
};
}
Expand Down Expand Up @@ -95,19 +95,23 @@ where
self.0.sig_parser.skip_char()?;
self.0.add_padding(u32::alignment(EncodingFormat::DBus))?;
let v = self.0.add_fd(v);
self.0.write_u32::<B>(v).map_err(Error::Io)
self.0
.write_u32::<B>(v)
.map_err(|e| Error::InputOutput(e.into()))
}
_ => {
self.0.prep_serialize_basic::<i32>()?;
self.0.write_i32::<B>(v).map_err(Error::Io)
self.0
.write_i32::<B>(v)
.map_err(|e| Error::InputOutput(e.into()))
}
}
}

fn serialize_u8(self, v: u8) -> Result<()> {
self.0.prep_serialize_basic::<u8>()?;
// Endianness is irrelevant for single bytes.
self.0.write_u8(v).map_err(Error::Io)
self.0.write_u8(v).map_err(|e| Error::InputOutput(e.into()))
}

serialize_basic!(serialize_u16(u16) write_u16);
Expand Down Expand Up @@ -140,10 +144,12 @@ where
.add_padding(<&str>::alignment(EncodingFormat::DBus))?;
self.0
.write_u32::<B>(usize_to_u32(v.len()))
.map_err(Error::Io)?;
.map_err(|e| Error::InputOutput(e.into()))?;
}
Signature::SIGNATURE_CHAR | VARIANT_SIGNATURE_CHAR => {
self.0.write_u8(usize_to_u8(v.len())).map_err(Error::Io)?;
self.0
.write_u8(usize_to_u8(v.len()))
.map_err(|e| Error::InputOutput(e.into()))?;
}
_ => {
let expected = format!(
Expand All @@ -161,15 +167,22 @@ where
}

self.0.sig_parser.skip_char()?;
self.0.write_all(v.as_bytes()).map_err(Error::Io)?;
self.0.write_all(&b"\0"[..]).map_err(Error::Io)?;
self.0
.write_all(v.as_bytes())
.map_err(|e| Error::InputOutput(e.into()))?;
self.0
.write_all(&b"\0"[..])
.map_err(|e| Error::InputOutput(e.into()))?;

Ok(())
}

fn serialize_bytes(self, v: &[u8]) -> Result<()> {
let seq = self.serialize_seq(Some(v.len()))?;
seq.ser.0.write(v).map_err(Error::Io)?;
seq.ser
.0
.write(v)
.map_err(|e| Error::InputOutput(e.into()))?;
seq.end()
}

Expand Down Expand Up @@ -237,7 +250,9 @@ where
self.0.add_padding(ARRAY_ALIGNMENT_DBUS)?;
// Length in bytes (unfortunately not the same as len passed to us here) which we
// initially set to 0.
self.0.write_u32::<B>(0_u32).map_err(Error::Io)?;
self.0
.write_u32::<B>(0_u32)
.map_err(|e| Error::InputOutput(e.into()))?;

let element_signature = self.0.sig_parser.next_signature()?;
let element_signature_len = element_signature.len();
Expand Down Expand Up @@ -344,13 +359,17 @@ where
.0
.writer
.seek(std::io::SeekFrom::Current(-total_array_len))
.map_err(Error::Io)?;
self.ser.0.writer.write_u32::<B>(len).map_err(Error::Io)?;
.map_err(|e| Error::InputOutput(e.into()))?;
self.ser
.0
.writer
.write_u32::<B>(len)
.map_err(|e| Error::InputOutput(e.into()))?;
self.ser
.0
.writer
.seek(std::io::SeekFrom::Current(total_array_len - 4))
.map_err(Error::Io)?;
.map_err(|e| Error::InputOutput(e.into()))?;

self.ser.0.container_depths = self.ser.0.container_depths.dec_array();

Expand Down
9 changes: 8 additions & 1 deletion zvariant/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{de, ser};
use static_assertions::assert_impl_all;
use std::{convert::Infallible, error, fmt, result};
use std::{convert::Infallible, error, fmt, result, sync::Arc};

/// Enum representing the max depth exceeded error.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -40,7 +40,10 @@ pub enum Error {
Message(String),

/// Wrapper for [`std::io::Error`](https://doc.rust-lang.org/std/io/struct.Error.html)
#[deprecated(note = "Use `Error::InputOutput` instead")]
Io(std::io::Error),
/// Wrapper for [`std::io::Error`](https://doc.rust-lang.org/std/io/struct.Error.html)
InputOutput(Arc<std::io::Error>),
/// Type conversions errors.
IncorrectType,
/// Wrapper for [`std::str::Utf8Error`](https://doc.rust-lang.org/std/str/struct.Utf8Error.html)
Expand Down Expand Up @@ -82,7 +85,9 @@ impl PartialEq for Error {
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
#[allow(deprecated)]
Error::Io(e) => Some(e),
Error::InputOutput(e) => Some(e),
Error::Utf8(e) => Some(e),
_ => None,
}
Expand All @@ -93,7 +98,9 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Message(s) => write!(f, "{}", s),
#[allow(deprecated)]
Error::Io(e) => e.fmt(f),
Error::InputOutput(e) => e.fmt(f),
Error::IncorrectType => write!(f, "incorrect type"),
Error::Utf8(e) => write!(f, "{}", e),
Error::PaddingNot0(b) => write!(f, "Unexpected non-0 padding byte `{}`", b),
Expand Down
2 changes: 1 addition & 1 deletion zvariant/src/framing_offset_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl FramingOffsetSize {
FramingOffsetSize::U64 => writer.write_u64::<LE>(offset as u64),
FramingOffsetSize::U128 => writer.write_u128::<LE>(offset as u128),
}
.map_err(Error::Io)
.map_err(|e| Error::InputOutput(e.into()))
}

pub fn read_last_offset_from_buffer(self, buffer: &[u8]) -> usize {
Expand Down
28 changes: 21 additions & 7 deletions zvariant/src/gvariant/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ where
self.0.container_depths = self.0.container_depths.dec_maybe();

if !fixed_sized_child {
self.0.write_all(&b"\0"[..]).map_err(Error::Io)?;
self.0
.write_all(&b"\0"[..])
.map_err(|e| Error::InputOutput(e.into()))?;
}
}
None => {
Expand Down Expand Up @@ -174,15 +176,22 @@ where
// Strings in GVariant format require no alignment.

self.0.sig_parser.skip_char()?;
self.0.write_all(v.as_bytes()).map_err(Error::Io)?;
self.0.write_all(&b"\0"[..]).map_err(Error::Io)?;
self.0
.write_all(v.as_bytes())
.map_err(|e| Error::InputOutput(e.into()))?;
self.0
.write_all(&b"\0"[..])
.map_err(|e| Error::InputOutput(e.into()))?;

Ok(())
}

fn serialize_bytes(self, v: &[u8]) -> Result<()> {
let seq = self.serialize_seq(Some(v.len()))?;
seq.ser.0.write(v).map_err(Error::Io)?;
seq.ser
.0
.write(v)
.map_err(|e| Error::InputOutput(e.into()))?;
seq.end()
}

Expand All @@ -198,7 +207,9 @@ where
}

fn serialize_unit(self) -> Result<()> {
self.0.write_all(&b"\0"[..]).map_err(Error::Io)
self.0
.write_all(&b"\0"[..])
.map_err(|e| Error::InputOutput(e.into()))
}

fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
Expand Down Expand Up @@ -516,11 +527,14 @@ where
value.serialize(&mut ser)?;
self.ser.0.bytes_written = ser.0.bytes_written;

self.ser.0.write_all(&b"\0"[..]).map_err(Error::Io)?;
self.ser
.0
.write_all(&b"\0"[..])
.map_err(|e| Error::InputOutput(e.into()))?;
self.ser
.0
.write_all(signature.as_bytes())
.map_err(Error::Io)?;
.map_err(|e| Error::InputOutput(e.into()))?;

Ok(())
}
Expand Down
6 changes: 4 additions & 2 deletions zvariant/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ where
if padding > 0 {
let byte = [0_u8; 1];
for _ in 0..padding {
self.write_all(&byte).map_err(Error::Io)?;
self.write_all(&byte)
.map_err(|e| Error::InputOutput(e.into()))?;
}
}

Expand Down Expand Up @@ -471,7 +472,8 @@ where
self.add_padding(alignment)?;

// Now serialize the veriant index.
self.write_u32::<B>(variant_index).map_err(Error::Io)?;
self.write_u32::<B>(variant_index)
.map_err(|e| Error::InputOutput(e.into()))?;

// Skip the `(`, `u`.
self.sig_parser.skip_chars(2)?;
Expand Down

0 comments on commit ae99095

Please sign in to comment.