#newtype #class #macro-derive #value

rvstruct

A helper macros implementation for Value Classes in Rust

6 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

#729 in Rust patterns

Download history 7452/week @ 2024-04-20 5912/week @ 2024-04-27 5891/week @ 2024-05-04 6164/week @ 2024-05-11 6488/week @ 2024-05-18 6211/week @ 2024-05-25 5939/week @ 2024-06-01 8198/week @ 2024-06-08 8346/week @ 2024-06-15 8838/week @ 2024-06-22 8346/week @ 2024-06-29 9141/week @ 2024-07-06 8073/week @ 2024-07-13 7052/week @ 2024-07-20 7734/week @ 2024-07-27 7678/week @ 2024-08-03

32,154 downloads per month
Used in 20 crates (8 directly)

Apache-2.0

6KB

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
~35K SLoC