Trait cov::intern::SerializeWithInterner[][src]

pub trait SerializeWithInterner {
    fn serialize_with_interner<S: Serializer>(
        &self,
        serializer: S,
        interner: &Interner
    ) -> Result<S::Ok, S::Error>; fn with_interner<'si>(
        &self,
        interner: &'si Interner
    ) -> WithInterner<'si, &Self> { ... } }

A data structure that may contain Symbols, which should be serialized as strings.

Required Methods

Serializes this value with help from an Interner that writes Symbols as strings.

If any member of this type is expected to contain a Symbol, they should be wrapped using with_interner(), so that deeply nested Symbols can be recursively found and transformed.

Examples

extern crate cov;
extern crate serde;
extern crate serde_json;
use cov::{Symbol, Interner, SerializeWithInterner};
use serde::ser::{Serializer, SerializeStruct};

struct SymbolCounters {
    symbols: Vec<Symbol>,
    counters: Vec<u32>,
}

impl SerializeWithInterner for SymbolCounters {
    fn serialize_with_interner<S: Serializer>(
        &self,
        serializer: S,
        interner: &Interner
    ) -> Result<S::Ok, S::Error> {
        let mut state = serializer.serialize_struct("SymbolCounters", 2)?;
        state.serialize_field("symbols", &self.symbols.with_interner(interner))?;
        // ^ note the use of `with_interner()`, since a Vec<Symbol> contains Symbol.
        state.serialize_field("counters", &self.counters)?;
        // ^ no need to call `with_interner()` for irrelevant types.
        state.end()
    }
}

// ...

let mut interner = Interner::new();
let s1 = interner.intern("one");
let s2 = interner.intern("two");

let symbol_counters = SymbolCounters {
    symbols: vec![s1, s2],
    counters: vec![45, 67],
};
let serialized = serde_json::to_string(&symbol_counters.with_interner(&interner))?;
assert_eq!(&serialized, r#"{"symbols":["one","two"],"counters":[45,67]}"#);

Provided Methods

Adorns this object with a string interner.

Returns a serializable object which writes out Symbols as strings instead of numbers.

Implementations on Foreign Types

impl<K: SerializeWithInterner + Ord, V: SerializeWithInterner> SerializeWithInterner for BTreeMap<K, V>
[src]

impl<K: SerializeWithInterner + Eq + Hash, V: SerializeWithInterner> SerializeWithInterner for HashMap<K, V>
[src]

impl<T: SerializeWithInterner> SerializeWithInterner for Vec<T>
[src]

impl<T: SerializeWithInterner> SerializeWithInterner for Option<T>
[src]

impl<'a, T: 'a + SerializeWithInterner + ?Sized> SerializeWithInterner for &'a T
[src]

impl SerializeWithInterner for u32
[src]

impl SerializeWithInterner for u64
[src]

impl SerializeWithInterner for usize
[src]

impl SerializeWithInterner for PathBuf
[src]

Implementors