#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

#179 in Rust patterns

Download history 926/week @ 2022-12-09 403/week @ 2022-12-16 188/week @ 2022-12-23 372/week @ 2022-12-30 676/week @ 2023-01-06 613/week @ 2023-01-13 754/week @ 2023-01-20 871/week @ 2023-01-27 1685/week @ 2023-02-03 895/week @ 2023-02-10 1188/week @ 2023-02-17 1126/week @ 2023-02-24 937/week @ 2023-03-03 1875/week @ 2023-03-10 857/week @ 2023-03-17 939/week @ 2023-03-24

4,709 downloads per month
Used in 10 crates (7 directly)

Apache-2.0

5KB

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.7–1.1MB
~27K SLoC