Module rapt::ser [] [src]

Serialization utilities

Serde Serializer utilities

This module provides a mechanism for instantiating new serializers over a given Write

Some Rapt's components depend on the ability to be parametrized over a serializer to use them interchangeably.

Unfortunately, it is impossible to uniformly instantiate different serializers. To ease this problem, this module will add optional integrations with known serializers.

Currently supported serializers are:

  • JsonSerializer — requires serde_json feature to be enabled; disabled by default

The technique employed in this module depends on a common pattern used in Serde ecosystem: actual serializers do not implement Serializer, but their mutable references (&mut) do.

This allows to use the serializer (as its trait uses self) and be able to retrieve the underlying writer.

If some of the serializers does not employ the mutable reference technique, perhaps, a wrapper can be implemented to fit it into the same model.

Example

extern crate rapt;
extern crate serde;

use serde::{Serialize, Serializer};
use rapt::ser::{InstantiateSerializer, IntoWriter, JsonSerializer};
pub fn test<IS, S>(is: IS) -> Vec<u8>
    where for<'a> IS: InstantiateSerializer<'a, Vec<u8>, Target=S>,
    S: IntoWriter<Vec<u8>>, for<'a> &'a mut S: Serializer {
   let mut ser = is.instantiate_serializer(Vec::with_capacity(256));
   let _ = "test".serialize(&mut ser).unwrap();
   ser.into_writer()
}
fn main() {
  assert!(test(JsonSerializer).len() > 0);
}

Traits

InstantiateSerializer

This trait instantiates a serializer over a given Write

IntoWriter

Converts value into a writer