简体   繁体   中英

impl vs derive in Rust

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM