Skip to content

Commit c2fbc55

Browse files
authored
Merge pull request #420 from filecoin-project/fix/remove-cbor-serde-error
fix: remove cbor serde error from everywhere
2 parents e3efae1 + e76412b commit c2fbc55

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

shared/src/encoding/cbor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub trait Cbor: ser::Serialize + de::DeserializeOwned {
2222

2323
/// Unmarshals cbor encoded bytes to object
2424
fn unmarshal_cbor(bz: &[u8]) -> Result<Self, Error> {
25-
Ok(from_slice(bz)?)
25+
from_slice(bz)
2626
}
2727

2828
/// Returns the content identifier of the raw block of data
@@ -101,6 +101,6 @@ impl RawBytes {
101101

102102
/// Deserializes the serialized bytes into a defined type.
103103
pub fn deserialize<O: de::DeserializeOwned>(&self) -> Result<O, Error> {
104-
Ok(from_slice(&self.bytes)?)
104+
from_slice(&self.bytes)
105105
}
106106
}

shared/src/encoding/mod.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ mod errors;
77
mod hash;
88
mod vec;
99

10+
use std::io;
11+
1012
pub use serde::{de, ser};
1113
pub use serde_bytes;
12-
pub use serde_ipld_dagcbor::{from_reader, from_slice, to_writer};
1314

1415
pub use self::bytes::*;
1516
pub use self::cbor::*;
@@ -29,8 +30,6 @@ pub mod repr {
2930
pub use serde_repr::{Deserialize_repr, Serialize_repr};
3031
}
3132

32-
// TODO: upstream this. Upstream doesn't allow encoding unsized types (e.g., slices).
33-
3433
/// Serializes a value to a vector.
3534
pub fn to_vec<T>(value: &T) -> Result<Vec<u8>, Error>
3635
where
@@ -40,3 +39,29 @@ where
4039
value.serialize(&mut serde_ipld_dagcbor::Serializer::new(&mut vec))?;
4140
Ok(vec)
4241
}
42+
43+
/// Decode a value from CBOR from the given reader.
44+
pub fn from_reader<T, R>(reader: R) -> Result<T, Error>
45+
where
46+
T: de::DeserializeOwned,
47+
R: io::Read,
48+
{
49+
serde_ipld_dagcbor::from_reader(reader).map_err(Into::into)
50+
}
51+
52+
/// Decode a value from CBOR from the given slice.
53+
pub fn from_slice<'a, T>(slice: &'a [u8]) -> Result<T, Error>
54+
where
55+
T: de::Deserialize<'a>,
56+
{
57+
serde_ipld_dagcbor::from_slice(slice).map_err(Into::into)
58+
}
59+
60+
/// Encode a value as CBOR to the given writer.
61+
pub fn to_writer<W, T>(writer: W, value: &T) -> Result<(), Error>
62+
where
63+
W: io::Write,
64+
T: ser::Serialize,
65+
{
66+
serde_ipld_dagcbor::to_writer(writer, value).map_err(Into::into)
67+
}

0 commit comments

Comments
 (0)