Rust won't compile the following:
#[derive(Copy, Clone)]
struct WhateverStruct<T> {
field : T
}
trait WhateverStructTrait<T> {
fn to(self) -> WhateverStruct<T>;
}
impl<T> WhateverStructTrait<T> for *mut WhateverStruct<T> {
fn to(self) -> WhateverStruct<T> {
unsafe {* self }
}
}
fn test() {
let x = WhateverStruct { field : 7u32 };
let _y = x;
let _z = x;
println!("Copying is fine");
}
fn main() {
test();
}
It complains on the unsafe {* self } part saying that
*selfhas typeWhateverStruct<T>, which does not implement theCopytrait
However, it quite clearly does implement the copy trait. The test function has no errors. If you change struct WhateverStruct<T> { field : T } into struct WhateverStruct { field : u32 } and remove the <T> from the rest of the code, everything compiles and runs just fine. So Rust isn't liking the generic.
Here you can see on the playground : https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5d5c3a0f0e6e0153ec286ce38f0f3a2d
Is this a bug? Is there a work around?