#value #class #newtype

macro rvs_derive

A helper macros implementation for Value Classes in Rust

10 releases

Uses new Rust 2021

0.3.0 Nov 4, 2021
0.2.3 Jun 19, 2020
0.1.4 Jun 1, 2020
0.1.3 May 27, 2020
0.1.1 Apr 30, 2020

#10 in #class

Download history 353/week @ 2022-06-03 326/week @ 2022-06-10 312/week @ 2022-06-17 264/week @ 2022-06-24 243/week @ 2022-07-01 342/week @ 2022-07-08 490/week @ 2022-07-15 327/week @ 2022-07-22 452/week @ 2022-07-29 360/week @ 2022-08-05 300/week @ 2022-08-12 352/week @ 2022-08-19 292/week @ 2022-08-26 347/week @ 2022-09-02 910/week @ 2022-09-09 859/week @ 2022-09-16

2,504 downloads per month
Used in 9 crates (via rvstruct)

Apache-2.0

6KB
128 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

~225–630KB
~15K SLoC