#newtype #class #value #macro-derive

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

#483 in Rust patterns

Download history 4779/week @ 2023-12-10 4126/week @ 2023-12-17 2079/week @ 2023-12-24 3130/week @ 2023-12-31 4618/week @ 2024-01-07 4416/week @ 2024-01-14 5178/week @ 2024-01-21 5200/week @ 2024-01-28 4756/week @ 2024-02-04 4711/week @ 2024-02-11 4603/week @ 2024-02-18 4575/week @ 2024-02-25 5585/week @ 2024-03-03 5137/week @ 2024-03-10 6054/week @ 2024-03-17 6189/week @ 2024-03-24

23,223 downloads per month
Used in 18 crates (7 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
~33K SLoC