pub struct MethodDef<'a> {
    pub name: Symbol,
    pub generics: Bounds,
    pub explicit_self: bool,
    pub nonself_args: Vec<(Ty, Symbol)>,
    pub ret_ty: Ty,
    pub attributes: AttrVec,
    pub unify_fieldless_variants: bool,
    pub combine_substructure: RefCell<Box<dyn FnMut(&mut ExtCtxt<'_>, Span, &Substructure<'_>) -> BlockOrExpr + 'a>>,
}

Fields

name: Symbol

name of the method

generics: Bounds

List of generics, e.g., R: rand::Rng

explicit_self: bool

Is there is a &self argument? If not, it is a static function.

nonself_args: Vec<(Ty, Symbol)>

Arguments other than the self argument.

ret_ty: Ty

Returns type

attributes: AttrVecunify_fieldless_variants: bool

Can we combine fieldless variants for enums into a single match arm? If true, indicates that the trait operation uses the enum tag in some way.

combine_substructure: RefCell<Box<dyn FnMut(&mut ExtCtxt<'_>, Span, &Substructure<'_>) -> BlockOrExpr + 'a>>

Implementations

The normal case uses field access.

#[derive(PartialEq)]
struct A { x: u8, y: u8 }

// equivalent to:
impl PartialEq for A {
    fn eq(&self, other: &A) -> bool {
        self.x == other.x && self.y == other.y
    }
}

But if the struct is repr(packed), we can’t use something like &self.x because that might cause an unaligned ref. So for any trait method that takes a reference, if the struct impls Copy then we use a local block to force a copy:

impl PartialEq for A {
    fn eq(&self, other: &A) -> bool {
        // Desugars to `{ self.x }.eq(&{ other.y }) && ...`
        { self.x } == { other.y } && { self.y } == { other.y }
    }
}
impl Hash for A {
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
        ::core::hash::Hash::hash(&{ self.x }, state);
        ::core::hash::Hash::hash(&{ self.y }, state)
    }
}

If the struct doesn’t impl Copy, we use let-destructuring with ref:

impl PartialEq for A {
    fn eq(&self, other: &A) -> bool {
        let Self { x: ref __self_0_0, y: ref __self_0_1 } = *self;
        let Self { x: ref __self_1_0, y: ref __self_1_1 } = *other;
        *__self_0_0 == *__self_1_0 && *__self_0_1 == *__self_1_1
    }
}

This latter case only works if the fields match the alignment required by the packed(N) attribute. (We’ll get errors later on if not.)

#[derive(PartialEq)]
enum A {
    A1,
    A2(i32)
}

is equivalent to:

impl ::core::cmp::PartialEq for A {
    #[inline]
    fn eq(&self, other: &A) -> bool {
        let __self_tag = ::core::intrinsics::discriminant_value(self);
        let __arg1_tag = ::core::intrinsics::discriminant_value(other);
        __self_tag == __arg1_tag &&
            match (self, other) {
                (A::A2(__self_0), A::A2(__arg1_0)) =>
                    *__self_0 == *__arg1_0,
                _ => true,
            }
    }
}

Creates a tag check combined with a match for a tuple of all selflike_args, with an arm for each variant with fields, possibly an arm for each fieldless variant (if !unify_fieldless_variants is not true), and possibly a default arm.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.

Layout

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference’s “Type Layout” chapter for details on type layout guarantees.

Size: 144 bytes