#newtype #class #value

macro rvs_derive

A helper macros implementation for Value Classes in Rust

11 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
0.1.1 Apr 30, 2020

#71 in #newtype

Download history 13006/week @ 2024-11-15 12060/week @ 2024-11-22 11858/week @ 2024-11-29 12423/week @ 2024-12-06 12140/week @ 2024-12-13 5696/week @ 2024-12-20 5625/week @ 2024-12-27 13122/week @ 2025-01-03 43231/week @ 2025-01-10 14832/week @ 2025-01-17 14334/week @ 2025-01-24 14827/week @ 2025-01-31 16352/week @ 2025-02-07 12512/week @ 2025-02-14 16208/week @ 2025-02-21 17581/week @ 2025-02-28

65,585 downloads per month
Used in 23 crates (via rvstruct)

Apache-2.0

9KB
137 lines

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