to_writer

Function to_writer 

Source
pub fn to_writer<'w, T, W>(wrtr: &'w mut W, val: &T) -> Result<()>
where T: FixedWidth + Serialize, W: 'w + Write,
Expand description

Serializes a type that implements FixedWidth to the given writer. Similar to to_writer_with_fields, but this function uses the fields defined in the trait implementation.

ยงExample

use serde_derive::Serialize;
use serde;
use fixed_width::{FixedWidth, Writer, FieldSet};

#[derive(Serialize)]
struct Person {
    pub name: String,
    pub age: usize,
}

impl FixedWidth for Person {
    fn fields() -> FieldSet {
        FieldSet::Seq(vec![
            FieldSet::new_field(0..8),
            FieldSet::new_field(8..10),
        ])
    }
}

let mut w = Writer::from_memory();

let person = Person {
    name: "coolname".to_string(),
    age: 25,
};

fixed_width::to_writer(&mut w, &person).unwrap();

let s: String = w.into();
assert_eq!("coolname25", s);