Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use specialization to improve performance #2147

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
)]
// Rustc lints.
#![deny(missing_docs, unused_imports)]

#![feature(specialization)]
////////////////////////////////////////////////////////////////////////////////

#[cfg(feature = "alloc")]
Expand Down
25 changes: 25 additions & 0 deletions serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,38 @@ mod content {
where
D: Deserializer<'de>,
{
<Content as DeserImpl<'de, D>>::deserialize_spec(deserializer)
}
}

trait DeserImpl<'de, D>: Sized
where
D: Deserializer<'de>,
{
fn deserialize_spec(deserializer: D) -> Result<Self, D::Error>;
}

impl<'de, D> DeserImpl<'de, D> for Content<'de>
where
D: Deserializer<'de>,
{
default fn deserialize_spec(deserializer: D) -> Result<Self, D::Error> {
// Untagged and internally tagged enums are only supported in
// self-describing formats.
let visitor = ContentVisitor { value: PhantomData };
deserializer.deserialize_any(visitor)
}
}

impl<'de, E> DeserImpl<'de, ContentDeserializer<'de, E>> for Content<'de>
where
ContentDeserializer<'de, E>: Deserializer<'de, Error = E>,
{
fn deserialize_spec(deserializer: ContentDeserializer<'de, E>) -> Result<Self, E> {
Ok(deserializer.content)
}
}

struct ContentVisitor<'de> {
value: PhantomData<Content<'de>>,
}
Expand Down