Skip to main content

read_reader

Function read_reader 

Source
pub fn read_reader(
    reader: impl Read,
    max_bytes: usize,
) -> Result<Vec<u8>, ArtifactError>
Expand description

Read one stream into bounded, fallibly allocated storage.

Observes at most max_bytes + 1 bytes to detect overflow. A blocking reader’s deadline remains caller-owned. Only interrupted reads are retried.

§Errors

Returns read, allocation, or byte-limit failures. An impossible reader byte count returns IO io::ErrorKind::InvalidData.

Examples found in repository?
examples/hash_serialized_json.rs (line 14)
7fn main() -> Result<(), Box<dyn Error>> {
8    let args: Vec<_> = env::args().collect();
9    if args.len() != 3 {
10        return Err("usage: hash_serialized_json INPUT_BYTES OUTPUT_BYTES < input.json".into());
11    }
12    let input_limit: usize = args[1].parse()?;
13    let output_limit: u64 = args[2].parse()?;
14    let input = read_reader(io::stdin().lock(), input_limit)?;
15    let value: serde_json::Value = serde_json::from_slice(&input)?;
16    let mut writer = BoundedWriter::new(Sha256::new(), output_limit);
17    serde_json::to_writer(&mut writer, &value)?;
18    let count = writer.bytes_written();
19    let digest = Sha256Digest::from_bytes(writer.into_inner().finalize().into());
20    println!("serialized_bytes={count} sha256={digest}");
21    Ok(())
22}