如何在 Rust 中获取实现 P: AsRef<Path> + ToString 接口的 Path 变量?
How to get a Path variable that implements P: AsRef<Path> + ToString interface in Rust?
我想使用 github 上某人提供的一些功能:
https://docs.rs/escposify/latest/escposify/device/struct.File.html#method.from_path
但是当我阅读 API 文档时,我遇到了这个函数的问题。它指出它需要实现 AsRef <Path>
和 ToString
.
的东西
pub fn from_path<P: AsRef<Path> + ToString>(path: P) -> File<File>
这是我的代码:
let path = Path::new("//192.168.100.8/Receipt Printer").to_string_lossy();
let file = File::from_path(path);
let mut printer = Printer::new(file, None, None);
它抛出一个错误:
error: the trait bound `Cow<'_, str>: AsRef<Path>` is not satisfied
label: required by a bound introduced by this call
note: required by a bound in `escposify::device::File::<W>::from_path`
label: required by a bound introduced by this call
这是我可以从 the source code 得到的实现:
pub fn from_path<P: AsRef<path::Path> + ToString>(path: P) -> File<fs::File> {
let fobj = fs::OpenOptions::new()
.write(true)
.create(true)
.open(&path)
.unwrap();
File { fobj }
}
在这种情况下,我应该如何放置我的文件路径 ("//192.168.100.8/Receipt Printer"
)?
&str
同时实现了 AsRef<Path>
和 ToString
,因此您应该能够 let path = "//192.168.100.8/Receipt Printer";
并直接使用它。
你应该直接使用 Path
,而不是 to_string_lossy()
……至少如果不是因为库的特征边界是错误的。
pub fn from_path<P: AsRef<path::Path> + ToString>(path: P) -> File<fs::File> {
let fobj = fs::OpenOptions::new()
.write(true)
.create(true)
.open(&path)
.unwrap();
File { fobj }
}
上面唯一使用 path
的行是 .open
行,调用 OpenOptions::open
,它只需要 AsRef<Path>
。 ToString
特性没有在该方法的任何地方使用,但它出现在通用绑定中的事实排除了将实际的 Path
传递给 from_path
,因为 Path
没有实现ToString
,因为它不能总是无损地转换为 UTF-8。
Path::to_string_lossy
除了在用户界面中标记文件以供参考外,不应使用其他用途。它不应该用于您以后可能实际需要访问的路径。
幸运的是,escposify 还提供了一个更通用的 File::from
trait 方法,它允许您自己打开一个文件并将其传递给库:
let path = Path::new("//192.168.100.8/Receipt Printer");
let fobj = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(&path)?;
let file = File::from(fobj);
let mut printer = Printer::new(file, None, None);
您也可以直接使用字符串文字,即像 建议的那样传递 &str
;对于使用硬编码路径很好的玩具程序,这应该足够了。
我想使用 github 上某人提供的一些功能:
https://docs.rs/escposify/latest/escposify/device/struct.File.html#method.from_path
但是当我阅读 API 文档时,我遇到了这个函数的问题。它指出它需要实现 AsRef <Path>
和 ToString
.
pub fn from_path<P: AsRef<Path> + ToString>(path: P) -> File<File>
这是我的代码:
let path = Path::new("//192.168.100.8/Receipt Printer").to_string_lossy();
let file = File::from_path(path);
let mut printer = Printer::new(file, None, None);
它抛出一个错误:
error: the trait bound `Cow<'_, str>: AsRef<Path>` is not satisfied
label: required by a bound introduced by this call
note: required by a bound in `escposify::device::File::<W>::from_path`
label: required by a bound introduced by this call
这是我可以从 the source code 得到的实现:
pub fn from_path<P: AsRef<path::Path> + ToString>(path: P) -> File<fs::File> {
let fobj = fs::OpenOptions::new()
.write(true)
.create(true)
.open(&path)
.unwrap();
File { fobj }
}
在这种情况下,我应该如何放置我的文件路径 ("//192.168.100.8/Receipt Printer"
)?
&str
同时实现了 AsRef<Path>
和 ToString
,因此您应该能够 let path = "//192.168.100.8/Receipt Printer";
并直接使用它。
你应该直接使用 Path
,而不是 to_string_lossy()
……至少如果不是因为库的特征边界是错误的。
pub fn from_path<P: AsRef<path::Path> + ToString>(path: P) -> File<fs::File> {
let fobj = fs::OpenOptions::new()
.write(true)
.create(true)
.open(&path)
.unwrap();
File { fobj }
}
上面唯一使用 path
的行是 .open
行,调用 OpenOptions::open
,它只需要 AsRef<Path>
。 ToString
特性没有在该方法的任何地方使用,但它出现在通用绑定中的事实排除了将实际的 Path
传递给 from_path
,因为 Path
没有实现ToString
,因为它不能总是无损地转换为 UTF-8。
Path::to_string_lossy
除了在用户界面中标记文件以供参考外,不应使用其他用途。它不应该用于您以后可能实际需要访问的路径。
幸运的是,escposify 还提供了一个更通用的 File::from
trait 方法,它允许您自己打开一个文件并将其传递给库:
let path = Path::new("//192.168.100.8/Receipt Printer");
let fobj = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(&path)?;
let file = File::from(fobj);
let mut printer = Printer::new(file, None, None);
您也可以直接使用字符串文字,即像 &str
;对于使用硬编码路径很好的玩具程序,这应该足够了。