我正在编写一个标记器,为方便起见,我编写了一个Reader
对象,该对象一次返回一个单词。当words
耗尽时,它会从BufReader
读取以填充words
。因此,我认为file
和words
都应该存在于该结构中。
我遇到的问题是我想通过传递要标记化的字符串来测试它,而不必依赖文件。这就是为什么我尝试同时在From
和File
和&str
上实现String
的原因。后两个不起作用(如下所示)。
我试图用生命周期注释Reader
,然后将其用于From<&'a str>
的实现,但这没有用。我最终得到了Reader<'a, T: Read>
,但是编译器提示没有任何东西使用了lifetime参数。From<&'static str>
的替代实现效果很好,但是这意味着在静态生存期中必须存在任何传入的字符串。
我也看到了这个question/answer,但是似乎有所不同,因为他们的Enum
具有有效期参数。
标题中有两个补充问题以及我的总体问题:
FromStr
,但是还没有尝试使用它-是否合适? 我的最小示例在这里(省略了导入):
#[derive(Debug)]
struct Reader<T: Read> {
file: BufReader<T>,
words: Vec<String>,
}
impl From<File> for Reader<File> {
fn from(value: File) -> Self { // value moves into from
Reader::new(BufReader::new(value)) // value moves into BufReader(?)
}
}
// THE NEXT TWO DON'T WORK
impl From<&str> for Reader<&[u8]> {
fn from(value: &str) -> Self { // Compiler can't know how long the underlying data lives
Reader::new(BufReader::new(value.as_bytes())) // The data may not live as long as BufReader
}
}
impl From<String> for Reader<&[u8]> {
fn from(value: String) -> Self { // value moves into from
Reader::new(BufReader::new(value.as_bytes())) // value doesn't move into BufReader or Reader
} // value gets dropped
}
impl<T: Read> Reader<T> {
fn new(input: BufReader<T>) -> Self {
Self {
file: input,
words: vec![],
}
}
}
最佳答案
&str
一个使用生命周期注释(playground)进行编译:
impl<'a> From<&'a str> for Reader<&'a [u8]> {
fn from(value: &'a str) -> Self {
Reader::new(BufReader::new(value.as_bytes()))
}
}
如注释中所讨论的,您只需要注释引用,而不必尝试将生存期注释合并到Reader
本身中。请注意,该方法不适用于
String
,因为from
的签名将其移入了函数,并且该函数无法返回属于局部变量的字节。您可以为&String
实现它,但随后也可以使用&str
。
关于rust - 如何将具有 `Read`特征的字符串形式的东西传递给 `From`的实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63646567/