Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upSpecialize for deserializing from &[u8] #119
Comments
|
When you say "specialized", do you mean, like, the Rust language feature "specialization"? If so, I'm not seeing how that is used in the json deserializer. |
|
Eventually once specialization is stable, yes. For now it is done with this trait by having the caller select the right entry point based on the type of data that they have - from_slice vs from_str vs from_reader vs from_iter. |
|
I'm not a huge fan of adding a non-standard Read trait for this :/ I also don't think that Bincode will benefit from this as much as Json would. Because everything that comes out of bincode must be owned, practically every structure that gets deserialized will be copied. |
|
I've used Bincode a lot for network communication and save-games and I've never written any code that would hit this optimization. In fact, I think you'd need to custom-implement the Serde Deserialize trait in order to get any benefit. |
|
Ah yeah, deserialize_str is the overwhelmingly common case for JSON but for Bincode it is deserialize_string. Good call. |
|
re-opening and adding to milestone: unknown. Some people doing custom deserialize implementations could benefit from this. But I think we should wait until stabilization stabilizes. |
|
What are the objections against using a trait for this in lieu of specialization? |
|
As I understand it, the trait would have to be exposed to the user right? I'd rather have Bincode use traits and types just from |
|
Yes but the vast majority of users never see it. Most people just use this function where they pass in a &[u8] and they get back a T. pub fn from_slice<T>(v: &[u8]) -> Result<T>
where T: de::Deserialize,
{
from_trait(read::SliceRead::new(v))
}Or this function where they pass in an pub fn from_reader<R, T>(rdr: R) -> Result<T>
where R: io::Read,
T: de::Deserialize,
{
from_iter(rdr.bytes())
}Really the only time a user would see this is in the rare case that someone stores a long-lived deserializer (typically a stream deserializer) in a struct, but that is almost nobody: struct MyStruct<R: serde_json::de::Read> {
the_goods: serde_json::StreamDeserializer<R, serde_json::Value>,
} |
|
Isn't this now resolved since we have the |
|
@dgriffen: yeah, this should be closed |
Like in the JSON deserializer, Bincode should have a specialized codepath for deserializing from &[u8]. This is an enormous improvement because it would no longer require copying strings into a temp buffer.