inline_newtype 0.1.1

A rust newtype macro inspired by kotlin's inline class.
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 10.20 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 193.13 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Homepage
  • falan/inline_newtype
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • falan

inline_newtype

A rust newtype macro inspired by kotlin's inline class. When we use

newtype!(NewTypeOne, u32);

It generate the struct

#[derive(Debug, Clone)]
    struct NewTypeOne {
        pub v: u32,
    }

for you. The v is the default public field.

use inline_newtype::newtype;
use std::path::PathBuf;
newtype!(UserHomeDirectory, PathBuf);
newtype!(UserRuntimeDirectory, PathBuf);
let user_home_directory = UserHomeDirectory { v: PathBuf::from("hello") };
let user_runtime_directory= UserRuntimeDirectory {v: PathBuf::from("hello")};
fn test_newtype_type_func(user_home_directory: UserHomeDirectory) -> UserHomeDirectory{
         user_home_directory
}
test_newtype_type_func(user_runtime_directory);  // mismatch type

You can aslo make the newtype public just adding the pub.

use inline_newtype::newtype;
use std::path::PathBuf;
newtype!(UserHomeDirectory, PathBuf, pub);

You also can change the field name if you want.

use inline_newtype::newtype;
use std::path::PathBuf;
newtype!(UserHomeDirectory, PathBuf, path_buf);
let user_home_directory = UserHomeDirectory { path_buf: PathBuf::from("hello")};
assert_eq!(user_home_directory.path_buf, PathBuf::from("hello"));

Transform from one newtype to another

use inline_newtype::newtype;
use std::path::PathBuf;
newtype!(UserHomeDirectory, PathBuf);
newtype!(UserRuntimeDirectory, PathBuf);
let user_home_directory = UserHomeDirectory { v: PathBuf::from("hello") };
let user_runtime_directory = UserRuntimeDirectory { v: PathBuf::from("hello") };
fn transform_user_home_to_runtime_directory(
    mut user_home_directory: UserHomeDirectory,
) -> UserRuntimeDirectory {
    let mut runtime_dir = user_home_directory.v;
    runtime_dir.push("runtime_dir");
    UserRuntimeDirectory { v: runtime_dir }
}

You can also using braces to declare the newtype.

use inline_newtype::newtype;
use std::path::PathBuf;
newtype! {UserHomeDirectory, PathBuf, path_buf}
let user_home_directory = UserHomeDirectory { path_buf: PathBuf::from("hello") };
assert_eq!(user_home_directory.path_buf, PathBuf::from("hello"))