Skip to content

Commit

Permalink
Hide zenoh_buffers::reader::DidntRead (#1447)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzypixelz authored Sep 18, 2024
1 parent a8e0b42 commit f2c3516
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 49 deletions.
10 changes: 0 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 42 additions & 39 deletions zenoh/src/api/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use uhlc::Timestamp;
use unwrap_infallible::UnwrapInfallible;
use zenoh_buffers::{
buffer::{Buffer, SplitBuffer},
reader::{DidntRead, HasReader, Reader},
reader::{HasReader, Reader},
writer::HasWriter,
ZBuf, ZBufReader, ZBufWriter, ZSlice, ZSliceBuffer,
};
Expand Down Expand Up @@ -473,13 +473,16 @@ impl ZBytes {
#[derive(Debug)]
pub struct ZBytesReader<'a>(ZBufReader<'a>);

#[derive(Debug)]
pub struct ReadError;

#[derive(Debug)]
pub enum ZReadOrDeserializeError<T>
where
T: TryFrom<ZBytes>,
<T as TryFrom<ZBytes>>::Error: Debug,
{
Read(DidntRead),
Read(ReadError),
Deserialize(<T as TryFrom<ZBytes>>::Error),
}

Expand Down Expand Up @@ -1778,34 +1781,34 @@ impl From<&mut Timestamp> for ZBytes {

impl Deserialize<Timestamp> for ZSerde {
type Input<'a> = &'a ZBytes;
type Error = zenoh_buffers::reader::DidntRead;
type Error = ReadError;

fn deserialize(self, v: Self::Input<'_>) -> Result<Timestamp, Self::Error> {
let codec = Zenoh080::new();
let mut reader = v.0.reader();
let e: Timestamp = codec.read(&mut reader)?;
let e: Timestamp = codec.read(&mut reader).map_err(|_| ReadError)?;
Ok(e)
}
}

impl TryFrom<ZBytes> for Timestamp {
type Error = zenoh_buffers::reader::DidntRead;
type Error = ReadError;

fn try_from(value: ZBytes) -> Result<Self, Self::Error> {
ZSerde.deserialize(&value)
}
}

impl TryFrom<&ZBytes> for Timestamp {
type Error = zenoh_buffers::reader::DidntRead;
type Error = ReadError;

fn try_from(value: &ZBytes) -> Result<Self, Self::Error> {
ZSerde.deserialize(value)
}
}

impl TryFrom<&mut ZBytes> for Timestamp {
type Error = zenoh_buffers::reader::DidntRead;
type Error = ReadError;

fn try_from(value: &mut ZBytes) -> Result<Self, Self::Error> {
ZSerde.deserialize(&*value)
Expand Down Expand Up @@ -1867,34 +1870,34 @@ impl From<&mut Encoding> for ZBytes {

impl Deserialize<Encoding> for ZSerde {
type Input<'a> = &'a ZBytes;
type Error = zenoh_buffers::reader::DidntRead;
type Error = ReadError;

fn deserialize(self, v: Self::Input<'_>) -> Result<Encoding, Self::Error> {
let codec = Zenoh080::new();
let mut reader = v.0.reader();
let e: EncodingProto = codec.read(&mut reader)?;
let e: EncodingProto = codec.read(&mut reader).map_err(|_| ReadError)?;
Ok(e.into())
}
}

impl TryFrom<ZBytes> for Encoding {
type Error = zenoh_buffers::reader::DidntRead;
type Error = ReadError;

fn try_from(value: ZBytes) -> Result<Self, Self::Error> {
ZSerde.deserialize(&value)
}
}

impl TryFrom<&ZBytes> for Encoding {
type Error = zenoh_buffers::reader::DidntRead;
type Error = ReadError;

fn try_from(value: &ZBytes) -> Result<Self, Self::Error> {
ZSerde.deserialize(value)
}
}

impl TryFrom<&mut ZBytes> for Encoding {
type Error = zenoh_buffers::reader::DidntRead;
type Error = ReadError;

fn try_from(value: &mut ZBytes) -> Result<Self, Self::Error> {
ZSerde.deserialize(&*value)
Expand Down Expand Up @@ -2628,17 +2631,17 @@ where
let codec = Zenoh080::new();
let mut reader = bytes.0.reader();

let abuf: ZBuf = codec
.read(&mut reader)
.map_err(|e| ZReadOrDeserializeErrorTuple2::One(ZReadOrDeserializeError::Read(e)))?;
let abuf: ZBuf = codec.read(&mut reader).map_err(|_| {
ZReadOrDeserializeErrorTuple2::One(ZReadOrDeserializeError::Read(ReadError))
})?;
let apld = ZBytes::new(abuf);
let a = A::try_from(apld).map_err(|e| {
ZReadOrDeserializeErrorTuple2::One(ZReadOrDeserializeError::Deserialize(e))
})?;

let bbuf: ZBuf = codec
.read(&mut reader)
.map_err(|e| ZReadOrDeserializeErrorTuple2::Two(ZReadOrDeserializeError::Read(e)))?;
let bbuf: ZBuf = codec.read(&mut reader).map_err(|_| {
ZReadOrDeserializeErrorTuple2::Two(ZReadOrDeserializeError::Read(ReadError))
})?;
let bpld = ZBytes::new(bbuf);
let b = B::try_from(bpld).map_err(|e| {
ZReadOrDeserializeErrorTuple2::Two(ZReadOrDeserializeError::Deserialize(e))
Expand Down Expand Up @@ -2824,25 +2827,25 @@ where
let codec = Zenoh080::new();
let mut reader = bytes.0.reader();

let abuf: ZBuf = codec
.read(&mut reader)
.map_err(|e| ZReadOrDeserializeErrorTuple3::One(ZReadOrDeserializeError::Read(e)))?;
let abuf: ZBuf = codec.read(&mut reader).map_err(|_| {
ZReadOrDeserializeErrorTuple3::One(ZReadOrDeserializeError::Read(ReadError))
})?;
let apld = ZBytes::new(abuf);
let a = A::try_from(apld).map_err(|e| {
ZReadOrDeserializeErrorTuple3::One(ZReadOrDeserializeError::Deserialize(e))
})?;

let bbuf: ZBuf = codec
.read(&mut reader)
.map_err(|e| ZReadOrDeserializeErrorTuple3::Two(ZReadOrDeserializeError::Read(e)))?;
let bbuf: ZBuf = codec.read(&mut reader).map_err(|_| {
ZReadOrDeserializeErrorTuple3::Two(ZReadOrDeserializeError::Read(ReadError))
})?;
let bpld = ZBytes::new(bbuf);
let b = B::try_from(bpld).map_err(|e| {
ZReadOrDeserializeErrorTuple3::Two(ZReadOrDeserializeError::Deserialize(e))
})?;

let cbuf: ZBuf = codec
.read(&mut reader)
.map_err(|e| ZReadOrDeserializeErrorTuple3::Three(ZReadOrDeserializeError::Read(e)))?;
let cbuf: ZBuf = codec.read(&mut reader).map_err(|_| {
ZReadOrDeserializeErrorTuple3::Three(ZReadOrDeserializeError::Read(ReadError))
})?;
let cpld = ZBytes::new(cbuf);
let c = C::try_from(cpld).map_err(|e| {
ZReadOrDeserializeErrorTuple3::Three(ZReadOrDeserializeError::Deserialize(e))
Expand Down Expand Up @@ -3053,33 +3056,33 @@ where
let codec = Zenoh080::new();
let mut reader = bytes.0.reader();

let abuf: ZBuf = codec
.read(&mut reader)
.map_err(|e| ZReadOrDeserializeErrorTuple4::One(ZReadOrDeserializeError::Read(e)))?;
let abuf: ZBuf = codec.read(&mut reader).map_err(|_| {
ZReadOrDeserializeErrorTuple4::One(ZReadOrDeserializeError::Read(ReadError))
})?;
let apld = ZBytes::new(abuf);
let a = A::try_from(apld).map_err(|e| {
ZReadOrDeserializeErrorTuple4::One(ZReadOrDeserializeError::Deserialize(e))
})?;

let bbuf: ZBuf = codec
.read(&mut reader)
.map_err(|e| ZReadOrDeserializeErrorTuple4::Two(ZReadOrDeserializeError::Read(e)))?;
let bbuf: ZBuf = codec.read(&mut reader).map_err(|_| {
ZReadOrDeserializeErrorTuple4::Two(ZReadOrDeserializeError::Read(ReadError))
})?;
let bpld = ZBytes::new(bbuf);
let b = B::try_from(bpld).map_err(|e| {
ZReadOrDeserializeErrorTuple4::Two(ZReadOrDeserializeError::Deserialize(e))
})?;

let cbuf: ZBuf = codec
.read(&mut reader)
.map_err(|e| ZReadOrDeserializeErrorTuple4::Three(ZReadOrDeserializeError::Read(e)))?;
let cbuf: ZBuf = codec.read(&mut reader).map_err(|_| {
ZReadOrDeserializeErrorTuple4::Three(ZReadOrDeserializeError::Read(ReadError))
})?;
let cpld = ZBytes::new(cbuf);
let c = C::try_from(cpld).map_err(|e| {
ZReadOrDeserializeErrorTuple4::Three(ZReadOrDeserializeError::Deserialize(e))
})?;

let dbuf: ZBuf = codec
.read(&mut reader)
.map_err(|e| ZReadOrDeserializeErrorTuple4::Four(ZReadOrDeserializeError::Read(e)))?;
let dbuf: ZBuf = codec.read(&mut reader).map_err(|_| {
ZReadOrDeserializeErrorTuple4::Four(ZReadOrDeserializeError::Read(ReadError))
})?;
let dpld = ZBytes::new(dbuf);
let d = D::try_from(dpld).map_err(|e| {
ZReadOrDeserializeErrorTuple4::Four(ZReadOrDeserializeError::Deserialize(e))
Expand Down

0 comments on commit f2c3516

Please sign in to comment.