[−][src]Function fixed_width::to_bytes
pub fn to_bytes<T: FixedWidth + Serialize>(record: &T) -> Result<Vec<u8>>
Serializes the given type that implements FixedWidth
and Serialize
to a String
.
Example
#[macro_use] extern crate serde_derive; extern crate serde; extern crate fixed_width; use fixed_width::{Field, FixedWidth}; #[derive(Serialize)] struct Record { pub name: String, pub room: usize, } impl FixedWidth for Record { fn fields() -> Vec<Field> { vec![ Field::default().range(0..4), Field::default().range(4..8), ] } } fn main() { let record = Record { name: "Carl".to_string(), room: 1234 }; let s = fixed_width::to_bytes(&record).unwrap(); assert_eq!(&s, b"Carl1234"); }