ciborium icon indicating copy to clipboard operation
ciborium copied to clipboard

[Bug]: Error when [de]serializing string

Open integral-llc opened this issue 2 years ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

Current Behaviour

Given the following code, I expect the unit test to pass

pub fn serialize<T: ?Sized>(value: &T) -> Result<Vec<u8>, Error>
where
    T: serde::ser::Serialize + Sized,
{
    let mut writer = Vec::new();
    into_writer(&value, &mut writer).unwrap();
    Ok(writer)
}

// Forward mc_util_serial::deserialize to bincode::deserialize
pub fn deserialize<'a, T: ?Sized>(bytes: &'a [u8]) -> Result<T, Error>
where
    T: serde::de::Deserialize<'a>,
{
    Ok(from_reader(bytes).unwrap())
}

#[test]
fn test_string() {
    let the_string = "There goes the baker with his tray, like always";
    let serialized = serialize(&the_string).unwrap();
    let deserialized: &str = deserialize(&serialized).unwrap();
    assert_eq!(deserialized, the_string);
}

the following error is displayed

called `Result::unwrap()` on an `Err` value: Semantic(None, "invalid type: string \"There goes the baker with his tray, like always\", expected a borrowed string")
thread 'test_string' panicked at 'called `Result::unwrap()` on an `Err` value: Semantic(None, "invalid type: string \"There goes the baker with his tray, like always\", expected a borrowed string")', ciborium/tests/no_std.rs:29:27
stack backtrace:

Expected Behaviour

unit tests shall pass

Environment Information

Darwin devxs-MacBook-Pro.local 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:46:32 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T6000 arm64

Steps To Reproduce

No response

integral-llc avatar Apr 06 '22 21:04 integral-llc

I don't think that can currently work for this crate and would require a new deserialization function. The reason is that &str would need to borrow from the input, which is not possible via the Read interface used by from_reader. The trait bounds on from_reader are simply wrong, see also #45.

jonasbb avatar Apr 06 '22 22:04 jonasbb