#limit #read #options

optional_take

std::io::Take的适配器,接受Option作为limit

1个不稳定版本

0.1.0 2022年1月10日

#75 in #limit

MIT/Apache

8KB
138

optional_take

optional_take::io

std::io::Take的适配器,接受Option<u64>作为limit。

示例

use optional_take::io::Takable;
use std::io::{self, Read};

fn main() -> io::Result<()> {
    let read = b"hello, world";
    let mut take = read.take_optional(Some(5));
    let mut buf = [0; 10];

    let len = take.read(&mut buf)?;
    assert_eq!(len, 5);
    assert_eq!(std::str::from_utf8(&buf[..len]).unwrap(), "hello");

    assert_eq!(take.read(&mut buf).unwrap(), 0);

    take.set_limit(None);
    let len = take.read(&mut buf)?;
    assert_eq!(len, 7);
    assert_eq!(std::str::from_utf8(&buf[..len]).unwrap(), ", world");

    Ok(())
}

许可证

optional_take在MIT许可证和Apache许可证(版本2.0)的条款下分发。

有关详细信息,请参阅LICENSE-APACHELICENSE-MIT

无运行时依赖