Coder Social home page Coder Social logo

struct-reuse's Introduction

Reuse - composable struct fields

An example of re-using struct fields across multiple structs in code by storing the source of structs across macros.

Usage

  1. Add this macro to your project
[dependencies]
reusable = "0.1.0"
  1. Add a #[reusable(name)] attribute to the structs you want to able to copy, where the name is a globally unique identifier for the struct (best to namespace using your crate and module names)

  2. Add a #[reuse(name)] attribute to the structs you want to reuse using the same name used in the attribute above

Example

use reusable::{reusable, reuse};

#[reusable(test_name)]
#[derive(Debug)]
struct Name {
    firstname: String,
    surname: String,
}

#[reuse(test_name)]
#[derive(Debug)]
struct Fullname {
    middlename: String,
}

fn main() {
    let example = Fullname {
        firstname: "Bob".to_string(),
        middlename: "Frank".to_string(),
        surname: "Junior".to_string(),
    };
    dbg!(example);
}

How it works

  1. This crate relies heavily on macro_state to share data between macro calls.

  2. The reusable attribute copies the tokenstream of a struct to a global state using the provided name as a key

  3. The reuse attribute reads the tokenstream set by reusable, parses the structs matching the names given and then appends the fields to the generated struct (any fields with the same name are skipped so can be overriden)

Note: multiple names can be provided to the reuse attribute, e.g. #[reuse(name1, name2)].

Works in stable Rust, no nightly required.

Alternatives

Other crates can provide similar functionality such as:

born - generates macros from inlined struct definitions that generate new structs

struct-reuse's People

Contributors

andrewlowndes avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.