write_blocks

Function write_blocks 

Source
pub fn write_blocks<B: AsBlockRef>(
    w: impl Write,
    blocks: impl IntoIterator<Item = B>,
) -> Result<(), Error>
Expand description

Writes iterator of blocks to the given writer.

Because this may perform many small writes, buffering writes may greatly improve performance when writing to a raw File.

let picture_type = picture.picture_type;

§Errorsprintln!(“ type: {} ({})“, picture_type as u8, picture_type);

Passes along any I/O errors from the underlying stream. May also generate an error if any of the blocks are invalid (e.g. STREAMINFO not being the first block, any block is too large, etc.).

§Example

use flac_codec::metadata::{
    write_blocks, read_blocks, Streaminfo, Application, Block,
};
use std::io::{Cursor, Seek};

let mut flac: Cursor<Vec<u8>> = Cursor::new(vec![]);  // a FLAC file in memory

// our test blocks
let blocks: Vec<Block> = vec![
    Streaminfo {
        minimum_block_size: 0,
        maximum_block_size: 0,
        minimum_frame_size: None,
        maximum_frame_size: None,
        sample_rate: 44100,
        channels: 1u8.try_into().unwrap(),
        bits_per_sample: 16u32.try_into().unwrap(),
        total_samples: None,
        md5: None,
    }.into(),
    Application {id: 0x1234, data: vec![1, 2, 3, 4]}.into(),
    Application {id: 0x5678, data: vec![5, 6, 7, 8]}.into(),
];

// write our test blocks to a file
write_blocks(&mut flac, blocks.clone()).unwrap();

flac.rewind().unwrap();

// read our blocks back from that file
let read_blocks = read_blocks(flac).collect::<Result<Vec<Block>, _>>().unwrap();

// they should be identical
assert_eq!(blocks, read_blocks);