-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can not use derive_more::From
with forward and derive_more::Into
at the same time
#198
Comments
This seems to be an known issue in Rust's trait system. We probably need negative trait bound to fix it. |
@xu-cheng yes, there is nothing we can do here from our side. Once Rust allows such things through specialization or anything else, these derives will support them too. |
@tyranron Well, we can actually workaround this issue like this: use std::collections::HashMap;
pub struct Foo(HashMap<i32, i32>);
impl<T> From<T> for Foo
where
HashMap<i32, i32>: From<T>,
{
#[inline]
fn from(original: T) -> Foo {
Foo(<HashMap<i32, i32> as From<T>>::from(original))
}
}
impl Into<HashMap<i32, i32>> for Foo {
#[inline]
fn into(self) -> HashMap<i32, i32> {
self.0
}
}
fn main() {
let hashmap: HashMap<i32, i32> = HashMap::from([(1, 1)]);
let foo = Foo::from(hashmap);
let hashmap2: HashMap<i32, i32> = foo.into();
} The only downside is that #[derive(
derive_more::From,
derive_more::Into,
)]
#[from(forward)]
#[into(into_only)]
pub struct Foo(HashMap<i32, i32>); |
@xu-cheng I do think it's not worth it. It's not that much of code to write by hand, if you really need it. But I'd argue that would be a good pattern in a code base. I'd rather go with providing a |
I agree with @tyranron |
The following code cannot be compiled:
I got the following error message:
FYI, this is the generated code obtained from
cargo expand
The text was updated successfully, but these errors were encountered: