-
Notifications
You must be signed in to change notification settings - Fork 3
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
Make serialization non-dependent on serde #107
Comments
One useful thing this would allow: a parameterizeable allowlist serializer. First, this setup would let you define a new marker type and enumerate a specific set of types which are allowed on the "allow list" denoted by that type. /// A marker type standing in for allowing every type to be serialized/deserialized.
pub struct AllowAll;
/// A marker trait that indicates whether or not the type `T` belongs to the allow-list `List`.
pub trait Allowed<List = AllowAll> {}
/// The `AllowAll` marker type allows all types.
impl<T> Allowed<AllowAll> for T {} You would use it like so: mod sealed {
pub struct OnlyByteAndBool;
}
impl Allowed<sealed::OnlyByteAndBool> for u8 {}
impl Allowed<sealed::OnlyByteAndBool> for bool {} The Then, this abstraction can be used to implement a general restriction mechanism for serializers/deserializers: #[repr(transparent)]
struct Restricted<T, List = AllowAll>{
inner: T,
list: PhantomData<fn() -> List>,
}
impl<S: Serializer, List> Serializer for Restricted<S, List> {
type Error = S::Error;
type Output = S::Output;
}
/// The where-clause on this impl restricts serialized types
/// to the allow-list specified in the phantom type
impl<List, T, S: SerializeFor<T>> SerializerFor<T> for Restricted<S, List>
where
T: Allowed<List>
{
#[inline(always)]
fn serialize(&mut self, item: &T) -> Result<Self::Output, Self::Error> {
self.inner.serialize(item)
}
}
impl<D: Deserializer, List> Deserializer for Restricted<D, List> {
type Error = S::Error;
}
/// The where-clause on this impl restricts deserialized types
/// to the allow-list specified in the phantom type
impl<List, Input, T, D: DeserializeFor<Input, T>> DeserializerFor<Input, T> for D
where
T: Allowed<List>
{
#[inline(always)]
fn deserialize(&mut self, src: &Input) -> Result<T, Self::Error> {
self.inner.deserialize(src)
}
} |
Also, with the unstable |
An alternate design would be to build the allow-list functionality directly into the |
As a separate issue, if this functionality is merged, the name of the crate is slightly misleading: |
Oh! A better design would be to use the allow-list construct to restrict arbitrary senders/receivers! Make a wrapper around a sender or receiver that uses the same technique, and it's more general than merely for serialization. |
Currently,
dialectic-tokio-serde
requires precisely aSerialize + DeserializeOwned
bound. This can be made more generic, to enable, for instance, custom serialization approaches or whitelists of allowed types.At present, the
Serializer
andDeserializer
traits are:This could be changed to:
This would separate the crate entirely from a dependency on Serde, allowing downstream crates to depend on it if necessary, but also to use their own serialization strategies while still sharing the framing/encoding logic in this crate. Additionally, it allows for the creation of wrapper Serializer/Deserializer structs which wrap an existing such Serializer/Deserializer but restrict its instance further, for instance to enforce a whitelist of permitted types.
The text was updated successfully, but these errors were encountered: