Skip to content
This repository has been archived by the owner on Aug 15, 2021. It is now read-only.

Implement de::from_slice_count #213

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ericmarkmartin
Copy link

Add a function that deserializes first valid cbor value out of a slice of bytes
and returns the number of consumed bytes.

Add a function that deserializes first valid cbor value out of a slice of bytes
and returns the number of consumed bytes.
@ericmarkmartin
Copy link
Author

@Firstyear and I would really like this functionality for our work in kanidm/webauthn-rs, where we need to parse cbor values out of byte streams.

@ericmarkmartin
Copy link
Author

I'm also somewhat curious about why I have to subtract one off of the error's offset.

Copy link

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This use case doesn't strike me as warranting a dedicated API inside serde_cbor. I would prefer to recommend that the caller uses the following code instead, which I find simple/clear.

let mut deserializer = Deserializer::from_slice(slice);
let value = T::deserialize(&mut deserializer)?;
let processed = deserializer.byte_offset();

@Firstyear
Copy link

Is there somewhere within the serde_cbor codebase where this could be added as an example usage?

@ericmarkmartin
Copy link
Author

ericmarkmartin commented Apr 6, 2021

Is there somewhere within the serde_cbor codebase where this could be added as an example usage?

Maybe in from_slice? @dtolnay, would you accept a PR adding such an example to the doc string?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants