pub enum MyEnum {
A,
B,
}
#[derive(Clone)]
struct UnsafeRef<T>(*const T);
type MyEnumRef = UnsafeRef<MyEnum>;
#[derive(Clone)]
struct UnsafeMyEnumRef(*const MyEnum);
fn main() {
let a = MyEnum::A;
let r = UnsafeRef::<MyEnum>(&a);
// This errors: the method `clone` exists for struct `UnsafeRef<MyEnum>`, but its trait bounds were not satisfied label: method cannot be called on `UnsafeRef<MyEnum>` due to unsatisfied trait bounds
let r2 = r.clone();
let r = UnsafeMyEnumRef(&a);
// This works
let r2 = r.clone();
}
How about deriving Clone
for MyEnum
?
The standard derive macros all assume that every type parameter on a struct needs to implement the trait as well, even when that obviously isn't true if you look at the struct fields. It's a known limitation.
For more about why it isn't quite as simple as "well don't do that then", see this blog post
1 Like
Don't want to implement clone on the enum ans especially don't want it to run by mistake. In my real life app some enum variants actually have huge values in them.
So any reason not just to do this?
ub enum MyEnum {
A,
B,
}
struct UnsafeRef<T>(*const T);
impl<T> Clone for UnsafeRef<T> {
fn clone(&self) -> Self {
Self(self.0)
}
}
type MyEnumRef = UnsafeRef<MyEnum>;
fn main() {
let a = MyEnum::A;
let r = UnsafeRef::<MyEnum>(&a);
let r2 = r.clone();
}
Nope, that should do exactly what you want
1 Like
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.