4 ms·
> open<P: AsRef<Path>>(path: P) Can't one achieve same with enum??
by ejanus 9y ago
> open<P: AsRef<Path>>(path: P)
Can't one achieve same with enum??
- kelnos 9y agoYes, but it's awkward to work with from the caller's side. You'd have to do something like this (hard to pick non-clashy names, too): enum Path { FromString(String), FromStr(&str), FromOsStr(OsStr), // ... } fn open(path: Path) ... { } And then as the caller you'd have to do things like: let file = open(Path::FromStr("/foo/bar")); It's not particularly nice to read, and you also have the overhead of creating and throwing away the enum instance.
- wtetzner 9y agoAn using AsRef means you can define your own types that can be used as paths, whereas an enum would be fixed once it was defined.
- Manishearth 9y agoYou then need to do extra wrapping, and other crates can't add new things that are "as reffable as a path", and there's a runtime penalty