impl vs derive in Rust

Viewed 46

I am new to Rust. I've found this code in Rust's compiler.

#[must_use]
pub struct IeeeFloat<S> {
    /// Absolute significand value (including the integer bit).
    sig: [Limb; 1],

    /// The signed unbiased exponent of the value.
    exp: ExpInt,

    /// What kind of floating point number this is.
    category: Category,

    /// Sign bit of the number.
    sign: bool,

    marker: PhantomData<S>,
}

Then they did

impl<S> Copy for IeeeFloat<S> {}
impl<S> Clone for IeeeFloat<S> {
    fn clone(&self) -> Self {
        *self
    }
}

My question is, isn't this same as #[derive(Copy, Clone)]? Why do they explicitly write impl Copy for IEEE? And isn't this clone implementation the same as the clone in std? Why would you redefine it

Link is here

1 Answers

No, this is not the same as deriving because the compiler won't make it generic enough. When you have a generic type T<U>, and try deriving Clone for it, it will only implement Clone for T<U> for U: Clone, even if T<U> could be cloned without having U: Clone. Note that it is the case for IeeeFloat<S>, since PhantomData<S>: Clone for all S.

Related