#value #class #newtype

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

#15 in #value

Download history 513/week @ 2022-11-27 485/week @ 2022-12-04 941/week @ 2022-12-11 338/week @ 2022-12-18 171/week @ 2022-12-25 401/week @ 2023-01-01 732/week @ 2023-01-08 713/week @ 2023-01-15 713/week @ 2023-01-22 890/week @ 2023-01-29 1787/week @ 2023-02-05 823/week @ 2023-02-12 1247/week @ 2023-02-19 973/week @ 2023-02-26 1409/week @ 2023-03-05 1586/week @ 2023-03-12

5,286 downloads per month
Used in 11 crates (via rvstruct)

Apache-2.0

8KB
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

~0.6–1MB
~26K SLoC