Readable Unix Permissions
Data types for the UNIX file permissions model. Convertable to and from mode_t
.
Usage
Crate is designed to give finer and more sane control over mode_t
, so there are multiple ways of using the Permissions
struct.
All the below functions are const fn
on nightly.
Creating from mode
let perms = from_mask; // Any value above 0u777 is truncated.
Creating through builder-pattern
You can add masks with add_mask()
let perms = new
.add_mask
.add_mask;
And remove them with remove_mask()
let perms = from_mask
.remove_mask
.remove_mask;
Checking
You can check which modes are set with has_mask()
.
let perms = from_mask;
assert!;
assert!;
assert!;
We also derive PartialEq<u32>
, to compare with mode_t
directly.
let perms = from_mask;
assert_eq!;
Extension trait
We also define an extension trait on target family unix
that follows std::os::unix::fs::PermissionsExt
.
See ext.rs for details.
use PermissionsExt as UnixPermsExt;
use PermissionsExt;
Performance
On nightly, most functions are const fn
incuring no runtime cost for constant definitions. On stable, not so. Either way, we define a global const
lookup table, so that conversions are as fast as a memory lookup.
Adding and removing the masks is usually 1 or two bitwise operations. TODO: Benchmark these?
Benchmarks
Type | Value |
---|---|
Conversions from mode | 159 ns/iter (+/- 15) |
Conversions to mode | 162 ns/iter (+/- 15) |
License
GPL'd with <3