我有这个lib.rs文件.
use std::io::{ Result, Read };
pub trait ReadExt: Read {
/// Read all bytes until EOF in this source, returning them as a new `Vec`.
///
/// See `read_to_end` for other semantics.
fn read_into_vec(&mut self) -> Result<Vec<u8>> {
let mut buf = Vec::new();
let res = self.read_to_end(&mut buf);
res.map(|_| buf)
}
/// Read all bytes until EOF in this source, returning them as a new buffer.
///
/// See `read_to_string` for other semantics.
fn read_into_string(&mut self) -> Result<String> {
let mut buf = String::new();
let res = self.read_to_string(&mut buf);
res.map(|_| buf)
}
}
impl<T> ReadExt for T where T: Read {}
Run Code Online (Sandbox Code Playgroud)
现在我想在一个单独的地方编写测试 test/lib.rs
extern crate readext;
use std::io::{Read,Cursor};
use readext::ReadExt;
#[test]
fn test () {
let bytes = b"hello";
let mut input = Cursor::new(bytes);
let s = input.read_into_string();
assert_eq!(s, "hello");
}
Run Code Online (Sandbox Code Playgroud)
但是Rust一直告诉我
type std::io::cursor::Cursor<&[u8; 5]>未实现命名范围内的任何方法read_into_string
我不知道为什么.显然我已经是use这样了.困惑.
答案已经在错误中:
输入std :: io :: cursor :: Cursor <&[u8; 5]>没有在名为read_into_string的作用域中实现任何方法
问题是,Cursor<&[u8; 5]>没有实现,Read因为包装类型是指向固定大小的数组而不是切片的指针,因此它也没有实现你的特征.我想这些内容应该有效:
#[test]
fn test () {
let bytes = b"hello";
let mut input = Cursor::new(bytes as &[u8]);
let s = input.read_into_string();
assert_eq!(s, "hello");
}
Run Code Online (Sandbox Code Playgroud)
这种方式input是Cursor<&[u8]>实现的类型,Read因此也应该实现您的特征.
| 归档时间: |
|
| 查看次数: |
426 次 |
| 最近记录: |