Skip to content

falan/inline_newtype

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

When we use rust newtype!(NewTypeOne, u32); It generate the struct

 #[derive(Debug, Clone)]
 struct NewTypeOne(u32);
 impl NewTypeOne {
    pub fn v(&self) -> u32 {
        self.0.clone()
    }
 }

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 (PathBuf::from("hello"));
let user_runtime_directory= UserRuntimeDirectory (PathBuf::from("hello"));
fn test_newtype_type_func(user_home_directory: UserHomeDirectory) -> UserHomeDirectory{
         user_home_directory
}

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(PathBuf::from("hello") );
let user_runtime_directory = UserRuntimeDirectory (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(runtime_dir)
}

About

Rust newtype macro

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages