Skip to content

Commit 234dea6

Browse files
jonasbbenarxbot
authored andcommitted
Use DeserializeOwned for from_reader
Based on #45 I saw that `from_reader` uses the wrong trait bounds for `T`. It does not support arbitrary types, but only those, which do not borrow from the input value. This can be expressed with `DeserializeOwned`. The `Read` trait always returns owned data and has no concept of borrowing from the input. Therefore, any type deserialize with the function also cannot borrow from the input. You see the same trait bound on `serde_json::from_reader`. The changes in `tests/` are simply to get the tests compiling afterwards.
1 parent e6593a0 commit 234dea6

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

ciborium/src/de/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ where
799799

800800
/// Deserializes as CBOR from a type with [`impl ciborium_io::Read`](ciborium_io::Read)
801801
#[inline]
802-
pub fn from_reader<'de, T: de::Deserialize<'de>, R: Read>(reader: R) -> Result<T, Error<R::Error>>
802+
pub fn from_reader<T: de::DeserializeOwned, R: Read>(reader: R) -> Result<T, Error<R::Error>>
803803
where
804804
R::Error: core::fmt::Debug,
805805
{

ciborium/tests/codec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ciborium::value::Value;
1010
use ciborium::{cbor, de::from_reader, ser::into_writer};
1111

1212
use rstest::rstest;
13-
use serde::{Deserialize, Serialize};
13+
use serde::{de::DeserializeOwned, Deserialize, Serialize};
1414

1515
macro_rules! val {
1616
($x:expr) => {
@@ -275,7 +275,7 @@ macro_rules! map {
275275
case(Enum::Tuple(56, 67), cbor!({"Tuple" => [56, 67]}).unwrap(), "a1655475706c658218381843", false, same), // Not In RFC
276276
case(Enum::Struct { first: 78, second: 89 }, cbor!({ "Struct" => { "first" => 78, "second" => 89 }}).unwrap(), "a166537472756374a2656669727374184e667365636f6e641859", false, same), // Not In RFC
277277
)]
278-
fn codec<'de, T: Serialize + Clone, V: Debug + PartialEq + Deserialize<'de>, F: Fn(T) -> V>(
278+
fn codec<'de, T: Serialize + Clone, V: Debug + PartialEq + DeserializeOwned, F: Fn(T) -> V>(
279279
input: T,
280280
value: Value,
281281
bytes: &str,

ciborium/tests/tag.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate alloc;
44

55
use ciborium::{de::from_reader, ser::into_writer, tag::*, value::Value};
66
use rstest::rstest;
7-
use serde::{Deserialize, Serialize};
7+
use serde::{de::DeserializeOwned, Serialize};
88

99
use core::fmt::Debug;
1010

@@ -20,7 +20,7 @@ use core::fmt::Debug;
2020
case(Accepted::<_, 6>(true), "c7f5", Value::Tag(7, Value::Bool(true).into()), false, false),
2121
case(Accepted::<_, 6>(true), "f5", Value::Bool(true), false, true),
2222
)]
23-
fn test<'de, T: Serialize + Deserialize<'de> + Debug + Eq>(
23+
fn test<T: Serialize + DeserializeOwned + Debug + Eq>(
2424
item: T,
2525
bytes: &str,
2626
value: Value,

0 commit comments

Comments
 (0)