#value #class #newtype

rvstruct

A helper macros implementation for Value Classes in Rust

5 releases

0.3.0 Nov 4, 2021
0.2.3 Jun 19, 2020
0.2.2 Jun 19, 2020
0.2.1 Jun 18, 2020
0.2.0 Jun 18, 2020

#390 in Rust patterns

Download history 349/week @ 2022-06-08 263/week @ 2022-06-15 266/week @ 2022-06-22 307/week @ 2022-06-29 184/week @ 2022-07-06 424/week @ 2022-07-13 496/week @ 2022-07-20 318/week @ 2022-07-27 480/week @ 2022-08-03 271/week @ 2022-08-10 287/week @ 2022-08-17 307/week @ 2022-08-24 315/week @ 2022-08-31 622/week @ 2022-09-07 1049/week @ 2022-09-14 499/week @ 2022-09-21

2,537 downloads per month
Used in 8 crates (7 directly)

Apache-2.0

3KB

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

~220–630KB
~15K SLoC