英文:
Forwarding `impl From` param to specialized function
问题
你有一些代码,希望创建一个函数,该函数可以接受实现了 From<(u32, u32)>
的内容,并将其传递给期望要么是 Foo
要么是 (u32, u32)
的函数。但你遇到了错误,因为没有实现 From<impl From<(u32, u32)>>
的 trait。要实现这个目标,你可以尝试以下方法:
pub fn print<T>(val: T)
where
T: Into<Foo> + Into<(u32, u32)>,
{
print_foo(val.into());
print_tuple(val.into());
}
这个修改将函数 print
定义为一个泛型函数,可以接受任何类型 T
,只要 T
实现了 Into<Foo>
和 Into<(u32, u32)>
这两个 trait。这样,它就可以接受既可以转换为 Foo
也可以转换为 (u32, u32)
的类型。
希望这对你有所帮助。
英文:
So I have some type
#[derive(Debug)]
pub struct Foo {
pub bar: u32,
pub baz: u32,
}
impl From<(u32, u32)> for Foo {
fn from(foo: (u32, u32)) -> Self {
Self {
bar: foo.0,
baz: foo.1,
}
}
}
My aim is to create a function that would receive something that implements From<(u32, u32)>
and will forward it further to function expecting either Foo
or (u32, u32)
. So it should be something like this
pub fn print(val: impl From<(u32, u32)>) {
print_foo(val.into());
print_tuple(val.into())
}
pub fn print_tuple(t: (u32, u32)) {
println!("Tuple: {:?}", t);
}
pub fn print_foo(foo: Foo) {
println!("Foo: {:?}", foo);
}
Unfortunately this will cause an error:
the trait `From<impl From<(u32, u32)>>` is not implemented for `Foo`
the trait `From<impl From<(u32, u32)>>` is not implemented for `(u32, u32)`
As I understand this wraps the impl From<_>
with From
due to into()
call.
My question is how to reach the goal of having a function that can receive either tuple or struct that can be constructed from it and pass it further to specialized functions.
答案1
得分: 4
impl From<T>
是一个相当无用的类型来接收值,它可以是任何东西,但 From
允许我们做的只是创建新的相同类型的项目,不能以任何有意义的方式使用我们已经拥有的值。相反,你想要的是使用 Into
的实现:
pub fn print<T: Into<(u32, u32)> + Into<Foo> + Clone>(val: T) {
print_foo(val.clone().into());
print_tuple(val.into())
}
注意,由于你想要传递相同的值两次,你必须添加 Copy
或 Clone
约束,或者要求 for<'a> &'a T: Into<Foo>
。
英文:
impl From<T>
is a pretty useless type to receive values of, it could be anything but all that From
lets us do is create new items of the same type, not use the value we have in any meaningful way. What you want instead is use an Into
implementation:
pub fn print<T: Into<(u32, u32)> + Into<Foo> + Clone>(val: T) {
print_foo(val.clone().into());
print_tuple(val.into())
}
Note since you want to pass the same value twice you have to either add a Copy
or Clone
bound as well or require for<'a> &'a T: Into<Foo>
instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论