我正在使用std::fs
中的函数,这些函数带有诸如path: impl AsRef<Path>
的参数。我希望使自己的函数具有多态性,以便它们也可以接受任何impl AsRef<Path>
而不是仅接受&str
。但是,所讨论的类似路径的对象必须存储在我的一个结构中。这意味着必须将其存储为Box<dyn AsRef<Path>>
以提供已知的大小。我正在努力将此装箱的值转换为std::fs
函数可以接受的任何值。
请考虑以下代码:
use std::path::Path;
fn main() {
fn polymorphic(_: impl AsRef<Path>) {}
let boxed: Box<dyn AsRef<Path>> = Box::new("/foo/bar");
polymorphic(/*???*/);
}
我用什么替换问号,以便我可以用polymorphic
呼叫"/foo/bar"
?
取消引用并重新引用Box
: