nix
nix copied to clipboard
Convert from Metadata::uid() to nix::unistd::Uid
Both Metadata::uid() and nix::unistd::Uid are both u32. An easy way of converting between the two would much cleaner code.
let md = src.metadata()?; set_permissions(&file, md.permissions())?; chown(file.as_path(), Some(md.uid()), Some(md.gid()));
vs:
let uid = md.uid() as uid_t; let gid = md.gid() as gid_t; chown (file.as_path(), Some(uid.into()), Some(gid.into());
I'm not very happy about using into/from for conversions to netwtypes. It can lead to less readbale code.
I think it'd be better to have our own extension trait for Metadata with same function names, just different return types for strongly typed values.
How about chown which takes a fs::Metadata struct as arguments? Easier to read for the user than discovering the way to get the proper argument is something like: let uid = Some(nix::unistd::Uid::from_raw(md.uid()));
For example, to set the UID but not the GID.
let md = src.metadata()?; chown(file.as_pathI(), Some(md), None())?;
It's a much cleaner interface, and I'd imagine helps out others who run into similar issues.
chown taking Metadata would be incredibly surprising and annoying for all people who are not trying to copy owner from elsewhere. I'm strongly against this.
Longer code is not always worse. Clear code is better than short code.
I'd be happy with documentation, really. A good example would be nice.
Took me several hours to figure out the solution here. Rust is absolutely horrid with this kind of thing (Path, PathBuf, String, str, os_str, etc. and how they interrelate is another great example).
I find it surprising that you had trouble finding it. I guess you're a complete newbie to Rust?
In chown doc you can clearly see chown accepts Uid which has from_raw method which accepts uid_t which is same as u32 which is returned by Metadata::uid()
You literally just need to click on type definitions to figure it out. With this approach you can find any conversion. I hope it helps you in the future but I'm also curious what cause you to not do that in the first place?
Honestly Rust doesn't have enough types. There's no Utf8CString for instance. But yes, some types didn't get enough love and some conversions are missing. What's even worse sometimes safe APIs are missing.