Derive Macro bagel::Ctor

source · []
#[derive(Ctor)]
{
    // Attributes available to this derive:
    #[ctor_const]
    #[phantom]
}
Expand description

Ctor: Get a constructor derived

The Ctor macro will take the fields in the order they are declared and generate a constructor, that is a YourStruct::new() function.

Example

use bagel::Ctor;

#[derive(Ctor)]
struct MyStruct {
    int: u32,
    unsigned_int: i32,
}

let ms = MyStruct::new(1, -1);
assert_eq!(ms.int, 1);
assert_eq!(ms.unsigned_int, -1);

Attributes

The following attributes are available:

  • #[ctor_const]: Will make your ctors constant
  • #[phantom]: Will skip the specified PhantomData field(s) in the constructor, automatically adding PhantomData in the requisite positions

Constant constructors

To make your constructors const, simply add the #[ctor_const] attribute to the top of your struct.

Example

use bagel::Ctor;

#[derive(Ctor)]
#[ctor_const]
pub struct MyConst {
    a: u8,
    b: u8,
}
// you can now use it in constant contexts
const MC: MyConst = MyConst::new(1, 2);