Skip to main content

GenStruct

Struct GenStruct 

Source
pub struct GenStruct<'a, P: Parent> { /* private fields */ }
Expand description

Builder to generate a struct. Defaults to a struct with named fields struct <Name> { <field>: <ty>, ... }

Implementations§

Source§

impl<'a, P: Parent> GenStruct<'a, P>

Source

pub fn make_zst(&mut self) -> &mut Self

Make the struct a zero-sized type (no fields)

Any fields will be ignored

generator
    .generate_struct("Foo")
    .make_zst()
    .add_field("bar", "u16")
    .add_field("baz", "String");

Generates:

struct Foo;
Source

pub fn make_tuple(&mut self) -> &mut Self

Make the struct fields unnamed

The names of any field will be ignored

generator
    .generate_struct("Foo")
    .make_tuple()
    .add_field("bar", "u16")
    .add_field("baz", "String");

Generates:

struct Foo(u16, String);
Source

pub fn make_pub(&mut self) -> &mut Self

Make the struct pub. By default the struct will have no visibility modifier and will only be visible in the current scope.

Source

pub fn inherit_generics(&mut self) -> &mut Self

Inherit the generic parameters of the parent type.

// given a derive on struct Bar<'a>
generator
    .generate_struct("Foo")
    .inherit_generics()
    .add_field("bar", "&'a str");

Generates:

// given a derive on struct Bar<'a>
struct Foo<'a> {
    bar: &'a str
}
Source

pub fn with_generics( &mut self, generics: impl IntoIterator<Item = Generic>, ) -> &mut Self

Append generic parameters to the type.

generator
    .generate_struct("Foo")
    .with_generics([Lifetime { ident: Ident::new("a", Span::call_site()), constraint: vec![] }.into()])
    .add_field("bar", "&'a str");

Generates:

struct Foo<'a> {
    bar: &'a str
}
Source

pub fn with_generic(&mut self, generic: Generic) -> &mut Self

Add a generic parameter to the type.

generator
    .generate_struct("Foo")
    .with_generic(Lifetime { ident: Ident::new("a", Span::call_site()), constraint: vec![] }.into())
    .add_field("bar", "&'a str");

Generates:

struct Foo<'a> {
    bar: &'a str
}
Source

pub fn with_derive(&mut self, derive: impl Into<Path>) -> &mut Self

Add a derive macro to the struct.

generator
    .generate_struct("Foo")
    .with_derive("Clone")
    .with_derive("Default")
    .with_derive(Path::from_iter(vec!["serde", "Deserialize"]));

Generates:

#[derive(Clone, Default, serde::Deserialize)]
struct Foo { }
Source

pub fn with_derives<T: Into<Path>>( &mut self, derives: impl IntoIterator<Item = T>, ) -> &mut Self

Add derive macros to the struct.

generator
    .generate_struct("Foo")
    .with_derives([
        "Clone".into(),
        "Default".into(),
        Path::from_iter(vec!["serde", "Deserialize"]),
    ]);

Generates:

#[derive(Clone, Default, serde::Deserialize)]
struct Foo { }
Source

pub fn with_attribute( &mut self, name: impl AsRef<str>, value: impl FnOnce(&mut StreamBuilder) -> Result, ) -> Result<&mut Self>

Add an attribute to the struct. For #[derive(...)], use with_derive instead.

generator
    .generate_struct("Foo")
    .with_attribute("serde", |b| {
        b.push_parsed("(rename_all = \"camelCase\")")?;
        Ok(())
    })?;

Generates:

#[serde(rename_all = "camelCase")]
struct Foo { }
Source

pub fn with_parsed_attribute( &mut self, attribute: impl AsRef<str>, ) -> Result<&mut Self>

Add a parsed attribute to the struct. For #[derive(...)], use with_derive instead.

generator
    .generate_struct("Foo")
    .with_parsed_attribute("serde(rename_all = \"camelCase\")")?;

Generates:

#[serde(rename_all = "camelCase")]
struct Foo { }
Source

pub fn with_attribute_stream( &mut self, attribute: impl Into<TokenStream>, ) -> &mut Self

Add a token stream as an attribute to the struct. For #[derive(...)], use with_derive instead.


let attribute = "serde(rename_all = \"camelCase\")".parse::<TokenStream>().unwrap();
generator
    .generate_struct("Foo")
    .with_attribute_stream(attribute);

Generates:

#[serde(rename_all = "camelCase")]
struct Foo { }
Source

pub fn add_field( &mut self, name: impl Into<String>, ty: impl Into<String>, ) -> FieldBuilder<'_, Self>

Add a field to the struct.

Names are ignored when the Struct’s fields are unnamed

generator
    .generate_struct("Foo")
    .add_field("bar", "u16")
    .add_field("baz", "String");

Generates:

struct Foo {
    bar: u16,
    baz: String,
};
Source

pub fn impl_for(&mut self, name: impl Into<StringOrIdent>) -> ImplFor<'_, Self>

Add an impl <name> for <struct>

Source

pub fn impl(&mut self) -> Impl<'_, Self>

Generate an impl <name> implementation. See Impl for more information.

Source

pub fn generate_impl(&mut self) -> Impl<'_, Self>

Generate an impl <name> implementation. See Impl for more information.

Alias for impl which doesn’t need a r# prefix.

Trait Implementations§

Source§

impl<'a, P: Parent> Drop for GenStruct<'a, P>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, P: Parent> Parent for GenStruct<'a, P>

Auto Trait Implementations§

§

impl<'a, P> Freeze for GenStruct<'a, P>

§

impl<'a, P> RefUnwindSafe for GenStruct<'a, P>
where P: RefUnwindSafe,

§

impl<'a, P> !Send for GenStruct<'a, P>

§

impl<'a, P> !Sync for GenStruct<'a, P>

§

impl<'a, P> Unpin for GenStruct<'a, P>

§

impl<'a, P> UnsafeUnpin for GenStruct<'a, P>

§

impl<'a, P> !UnwindSafe for GenStruct<'a, P>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.