#value #class #newtype

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

#238 in Rust patterns

Download history 3872/week @ 2023-08-08 3900/week @ 2023-08-15 4167/week @ 2023-08-22 3706/week @ 2023-08-29 3770/week @ 2023-09-05 3517/week @ 2023-09-12 4291/week @ 2023-09-19 4538/week @ 2023-09-26 4812/week @ 2023-10-03 4606/week @ 2023-10-10 5387/week @ 2023-10-17 4394/week @ 2023-10-24 4351/week @ 2023-10-31 4355/week @ 2023-11-07 4630/week @ 2023-11-14 3444/week @ 2023-11-21

17,557 downloads per month
Used in 17 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
~31K SLoC