rvs_derive

Crates.iorvs_derive
lib.rsrvs_derive
version0.3.2
sourcesrc
created_at2020-04-30 10:03:37.249942
updated_at2022-12-11 12:58:28.524242
descriptionA helper macros implementation for Value Classes in Rust
homepagehttps://github.com/abdolence/rust-value-struct
repositoryhttps://github.com/abdolence/rust-value-struct
max_upload_size
id235764
size8,163
Abdulla Abdurakhmanov (abdolence)

documentation

https://github.com/abdolence/rust-value-struct

README

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

Commit count: 40

cargo fmt