#newtype #class #macro-derive #value

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

#941 in Rust patterns

Download history 7012/week @ 2024-07-22 7694/week @ 2024-07-29 7924/week @ 2024-08-05 8688/week @ 2024-08-12 9690/week @ 2024-08-19 10289/week @ 2024-08-26 10219/week @ 2024-09-02 11355/week @ 2024-09-09 11461/week @ 2024-09-16 11840/week @ 2024-09-23 12103/week @ 2024-09-30 11377/week @ 2024-10-07 11144/week @ 2024-10-14 12955/week @ 2024-10-21 10865/week @ 2024-10-28 11211/week @ 2024-11-04

46,592 downloads per month
Used in 22 crates (8 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
~37K SLoC