set

Macro set 

Source
macro_rules! set {
    ($fn_vis:vis $fn_name:ident => $field_name:ident, $value_type:ty) => { ... };
    ($fn_vis:vis $name:ident => $value_type:ty) => { ... };
    ($fn_vis:vis $fn_name:ident => $field_name:ident, into $value_type:ty) => { ... };
    ($fn_vis:vis $name:ident => into $value_type:ty) => { ... };
    ($fn_vis:vis $fn_name:ident => $field_name:ident, optional $value_type:ty) => { ... };
    ($fn_vis:vis $name:ident => optional $value_type:ty) => { ... };
    ($fn_vis:vis $fn_name:ident => $field_name:ident, optional into $value_type:ty) => { ... };
    ($fn_vis:vis $name:ident => optional into $value_type:ty) => { ... };
}
Expand description

Generate a setter method for a field within a structure.

§Forms

§set!(viz name => Type)

This form generates a simple setter function.

  • In this form name is both the name of the generated function and the name of the structure’s field.
  • This form requires mutability in the form of a mutable reference to self; &mut self.
  • The type of the new value parameter is the value type Type.
  • This function returns no value.

The following — commented line and following implementation — are therefore equivalent:

impl Address {
    // set!(pub number_on_street => u32);

    /// Set the value of the field `number_on_street` within this structure.
    pub fn number_on_street(&mut self, number_on_street: u32) {
        self.number_on_street = number_on_street;
    }
}

§set!(viz setter_name => field_name, Type)

This form generates a simple setter function.

  • In this form name is the name of the generated function while field_name is the name of the structure’s field.
  • This form requires mutability in the form of a mutable reference to self; &mut self.
  • The type of the new value parameter is the value type Type.
  • This function returns no value.
impl Address {
    // set!(pub number_on_street => number, u32);

    /// Set the value of the field `number_on_street` within this structure.
    pub fn number_on_street(&mut self, number_on_street: u32) {
        self.number = number_on_street;
    }
}

The following — commented line and following implementation — are therefore equivalent:

§set!(viz name => into Type)

This form generates a simple setter function.

  • In this form name is both the name of the generated function and the name of the structure’s field.
  • This form requires mutability in the form of a mutable reference to self; &mut self.
  • The type of the new value parameter is the trait-bound type T: Into<Type> rather than Type for flexibility.
  • This function returns no value.

The following — commented line and following implementation — are therefore equivalent:

impl Address {
    // set!(pub street_1 => into String);

    /// Set the value of the field `street_1` within this structure.
    pub fn street_1<T: Into<String>>(&mut self, street_1: T) {
        self.street_1 = street_1.into();
    }
}

§set!(viz setter_name => field_name, into Type)

This form generates a simple setter function.

  • In this form name is the name of the generated function while field_name is the name of the structure’s field.
  • This form requires mutability in the form of a mutable reference to self; &mut self.
  • The type of the new value parameter is the trait-bound type T: Into<Type> rather than Type for flexibility.
  • This function returns no value.

The following — commented line and following implementation — are therefore equivalent:

impl Address {
    // set!(pub street_1 => street_1_string, into String);

    /// Set the value of the field `street_1` within this structure.
    pub fn street_1<T: Into<String>>(&mut self, street_1: T) {
        self.street_1_string = street_1.into();
    }
}

§set!(viz name => optional Type)

This form generates a simple setter function, for optional fields.

  • In this form name is both the name of the generated function and the name of the structure’s field.
  • This form requires mutability in the form of a mutable reference to self; &mut self.
  • While the type of the structure’s field is Option<Type>, the type of the new value parameter is simply Type. The use of this macro is intended to be paired with an unset implementation.
  • This function returns no value.

The following — commented line and following implementation — are therefore equivalent:

impl Address {
    // set!(pub street_2 => optional String);

    /// Set the value of the field `street_2` within this structure. While the corresponding
    /// structure's field is an `Option<String>`, this function uses the type `String`. To
    /// set the field value to `None` use the method [`unset_street_2`].
     pub fn street_2(&mut self, street_2: String) {
        self.street_2 = Some(street_2);
    }
}

§set!(viz setter_name => field_name, optional Type)

This form generates a simple setter function, for optional fields.

  • In this form name is the name of the generated function while field_name is the name of the structure’s field.
  • This form requires mutability in the form of a mutable reference to self; &mut self.
  • While the type of the structure’s field is Option<Type>, the type of the new value parameter is simply Type. The use of this macro is intended to be paired with an unset implementation.
  • This function returns no value.

The following — commented line and following implementation — are therefore equivalent:

impl Address {
    // set!(pub street_2 => street_2_string, optional String);

    /// Set the value of the field `street_2` within this structure. While the corresponding
    /// structure's field is an `Option<String>`, this function uses the type `String`. To
    /// set the field value to `None` use the method [`unset_street_2`].
    pub fn street_2(&mut self, street_2: String) {
        self.street_2_string = Some(street_2);
    }
}

§set!(viz name => optional into Type)

This form generates a simple setter function, for optional fields.

  • In this form name is both the name of the generated function and the name of the structure’s field.
  • This form requires mutability in the form of a mutable reference to self; &mut self.
  • While the type of the structure’s field is Option<Type>, the type of the new value parameter is simply Type. The use of this macro is intended to be paired with an unset implementation.
  • This function returns no value.

The following — commented line and following implementation — are therefore equivalent:

impl Address {
    // set!(pub street_2 => optional into String);

    /// Set the value of the field `street_2` within this structure. While the corresponding
    /// structure's field is an `Option<String>`, this function uses the type `String`. To
    /// set the field value to `None` use the method [`unset_street_2`].
     pub fn street_2<T: Into<String>>(&mut self, street_2: T) {
        self.street_2 = Some(street_2.into());
    }
}

§set!(viz setter_name => field_name, optional into Type)

This form generates a simple setter function, for optional fields.

  • In this form name is the name of the generated function while field_name is the name of the structure’s field.
  • This form requires mutability in the form of a mutable reference to self; &mut self.
  • While the type of the structure’s field is Option<Type>, the type of the new value parameter is simply Type. The use of this macro is intended to be paired with an unset implementation.
  • This function returns no value.

The following — commented line and following implementation — are therefore equivalent:

impl Address {
    // set!(pub street_2 => street_2_string, optional into String);

    /// Set the value of the field `street_2` within this structure. While the corresponding
    /// structure's field is an `Option<String>`, this function uses the type `String`. To
    /// set the field value to `None` use the method [`unset_street_2`].
    pub fn street_2<T: Into<String>>(&mut self, street_2: T) {
        self.street_2_string = Some(street_2.into());
    }
}