August Feng

A study of newtype usage in Rust

The std::fs::FileType is a newtype that wraps an OS specific implementation of crate::sys::fs::FileType.

I've quoted the implementation of the std::fs::FileType (in library/std/src/fs.rs).

Notice that the newtype is a tuple struct, and we access the actual type by indexing into the tuple.

pub struct FileType(fs_imp::FileType);

impl FileType {
    pub fn is_dir(&self) -> bool {
        self.0.is_dir()
    }

    pub fn is_file(&self) -> bool {
        self.0.is_file()
    }

    pub fn is_symlink(&self) -> bool {
        self.0.is_symlink()
    }
}