我想实现一个扩展特征std::io::Read
来读取任意数字。但是我无法找到如何使用num_traits
板条箱获取任意数字的字节数(或者其他板条箱可能提供此信息):
use num_traits::FromBytes;
use std::io::{Read, Result};
pub trait ReadNums: Read {
fn read_num_be<N>(&mut self) -> Result<N>
where
N: FromBytes,
{
let mut buffer = [0; N::WHAT_HERE];
self.read_exact(&mut buffer)?;
Ok(N::from_be_bytes(&buffer))
}
}
impl<T> ReadNums for T where T: Read {}
[package]
name = "numtrait_test"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
num-traits = "0.2.17"
我不知道该放什么N::WHAT_HERE
。N
我需要此处以字节为单位的相应大小作为const
. 对于我的用例来说,使用夜间功能是不可能的。