Const OsString::new, PathBuf::new

Can std::ffi::os_str::OsString::new as well as std::path::PathBuf::new be const fn? Afaik they use String as inner and String::new is already const, so there shouldn't be any reason why they can't be const.

Thank you.

1 Like

(Not really relevant to your request, but) they don't use String because they support non-UTF-8.

3 Likes

A quick look at the internals reveals no technical reason this cannot be implemented. Windows uses WTF-8, which is Vec<u8> plus a bool indicating if it's known to be UTF-8. Everything else uses Vec<u8> (that is UTF-8).

2 Likes

A superset of UTF-8.

4 Likes

Creating empty string-like containers should always be able to be const so I see no reason OsString and PathBuf should be different whatever their internal representation.

Aside: on all platforms, OsStr must be a superset of UTF-8 due to the existing conversion methods being documented as zero cost (e.g. &str -> &OsStr). Therefore for all platform it will always be possible to have a const UTF-8 path even if non UTF-8 requires some special handling.

The promise is a bit stronger than that even.