我如何从Box >?中提取impl AsRef 值

问题描述 投票:0回答:1

我正在使用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"

rust traits
1个回答
1
投票

取消引用并重新引用Box

© www.soinside.com 2019 - 2024. All rights reserved.