#newtype #class #value #macro-support #strongly-typed

macro rvs_derive

A helper macros implementation for Value Classes in Rust

11 releases

0.3.2 Dec 11, 2022
0.3.1 Dec 11, 2022
0.3.0 Nov 4, 2021
0.2.3 Jun 19, 2020
0.1.1 Apr 30, 2020

#20 in #macro-support

Download history 3696/week @ 2023-11-23 4891/week @ 2023-11-30 4679/week @ 2023-12-07 4389/week @ 2023-12-14 2649/week @ 2023-12-21 2888/week @ 2023-12-28 4194/week @ 2024-01-04 4414/week @ 2024-01-11 4854/week @ 2024-01-18 5345/week @ 2024-01-25 4796/week @ 2024-02-01 4904/week @ 2024-02-08 4627/week @ 2024-02-15 4378/week @ 2024-02-22 5418/week @ 2024-02-29 3528/week @ 2024-03-07

18,952 downloads per month
Used in 19 crates (via rvstruct)

Apache-2.0

9KB
137 lines

Cargo

Value Structs derive macros for Rust to support the newtype pattern

Motivation

A very simple derive macros to support strong type system and the newtype pattern. Newtypes are a zero-cost abstraction: they introduce a new, distinct name for an existing type, with no runtime overhead when converting between the two types. This is a similar approach to Haskell's newtype keyword.

For example:

#[derive(ValueStruct)]
struct UserId(String);

let uid : UserId = "my-uid".into();

Macros overview

ValueStruct generates for you:

  • std::convert::From<> instances automatically to help you to create your structs.
  • ValueStruct::value() function implementation to access your field directly without using .0.

There are different behaviour for different field types:

  • For std::string::String it generates From<String>, From<&String>, From<&str>

Usage

Add this to your Cargo.toml:

[dependencies]
rvstruct = "0.3"
// Import it
use rvstruct::ValueStruct;

// And use it on your structs
#[derive(ValueStruct)]
struct UserId(String);

Licence

Apache Software License (ASL)

Author

Abdulla Abdurakhmanov

Dependencies

~1.5MB
~33K SLoC