Open
Conversation
dtolnay
requested changes
Mar 10, 2024
Owner
dtolnay
left a comment
There was a problem hiding this comment.
Thanks for the PR.
One thing that would need to be figured out before exposing these traits is how errors from the underlying serializer are returned. For example if serialize_map or serialize_entry fails. Thoughts?
|
fwiw, I had to do the following so I could delegate the serialization to a dynamic vtable: impl<'a> serde::Serialize for Object<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer
{
let mut erased = erased_serde::SerializerImpl::new(serializer);
match (self.0.vtable.serialize)(self, &mut erased) {
Ok(()) => {}
Err(err) => {
let err: erased_serde::ErrorImpl = err.into();
match err {
erased_serde::ErrorImpl::Custom(msg) => return Err(serde::ser::Error::custom(msg)),
erased_serde::ErrorImpl::ShortCircuit => {}
}
}
}
match erased {
erased_serde::SerializerImpl::Complete(ok) => Ok(ok),
erased_serde::SerializerImpl::Error(err) => Err(err),
_ => unreachable!(),
}
}
}This meant I had to copy the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I am working on a library that I would like the library to remain format-agnostic. To do this I am using
erased-serdeto "inject" the format at runtime as a&mut dyn Serializer.In my library the user defines a schema which looks something like this and the
resolvefunction would be implemented by them:As discussed in #39
erased-serdeisn't something that should be in the public API so I am wrapping all of its types. This also has the added side-effect that I can requireselfinstead of&mut selffor extra type safety.An example of what I am doing is below.
However, right now I can only deal with primitive types as
SerializeMapand similar types are private. This PR makes them public so the following code would compile.