libs-team: New failable API for SmartPointer to SmartPointer, etc
Proposal
Problem statement
cc uses Arc<OsStr> a lot (for env and flags) and we sometimes need them converted to a string.
Since these Arcs are wrapped with lock, it can’t return &str but would have to convert them to String.
Motivating examples or use cases
https://github.com/rust-lang/cc-rs/blob/6fa9ea631509f0dc2f6373121e2ec9db4e5f8001/src/lib.rs#L3558
while we can always convert it to String it is less efficient and less ergonomic over an API to convert Arc<OsStr> to Arc<str> in a faillable way.
Solution sketch
impl TryFrom<Arc<OsStr>> for Arc<str> {
type Err = Utf8Error;
fn try_from(os_str: Arc<OsStr>) -> Self;
}
impl TryFrom<Rc<OsStr>> for Rc<str> {
type Err = Utf8Error;
fn try_from(os_str: Rc<OsStr>) -> Self;
}
impl TryFrom<Box<OsStr>> for Box<str> {
type Err = Utf8Error;
fn try_from(os_str: Box<OsStr>) -> Self;
}
impl From<Arc<OsStr>> for Arc<Path> {}
impl From<Rc<OsStr>> for Rc<Path> {}
impl From<Box<OsStr>> for Box<Path> {}
impl From<Arc<str>> for Arc<OsStr> {}
impl From<Rc<str>> for Rc<OsStr> {}
impl From<Box<str>> for Box<OsStr> {}
impl From<Arc<Path>> for Arc<OsStr> {}
impl From<Rc<Path>> for Rc<OsStr> {}
impl From<Box<Path>> for Box<OsStr> {}
Alternatives
While the proposed API would fix it for smart pointers in stdlib, it won’t work for custom smart pointers.
And adding such APIs to every string type is annoying and it doesn’t scale, makes it harder to write custom smart pointers.
Perhaps rust should consider introduce a trait for smart pointers to make such conversion trivial:
trait SafeCastable<T>
where
T: ?Sized,
layout(T) == layout(Self),
{
fn cast(self) -> T;
}
impl<T, U> SafeCastable<U> for T
where
T: AsRef<U> + ?Sized,
U: ?Sized,
layout(T) == layout(U),
{...}
impl<T, U> SafeCastable<U> for T
where
T: Into<U> + ?Sized,
U: ?Sized,
layout(T) == layout(U),
{...}
trait SafeTryCastable<T>
where
T: ?Sized,
layout(T) == layout(U),
{
type R: Try<T>;
fn cast(self) -> R;
}
impl<T, U> SafeTryCastable<U> for T
where
T: TryInto<U> + ?Sized,
U: ?Sized,
layout(T) == layout(U),
{...}
trait CastableSmartPtr {
type Item: ?Sized;
type SmartPtr<T: ?Sized>;
unsafe fn cast_unchecked<T>(self) -> Self::SmartPtr<T>
where
T: ?Sized,
layout(T) == layout(Self::Item); // either need const generics or compiler-generated auto-imlemented traits
// provided methods
fn cast<T>(self) -> Self::SmartPtr<T>
where
T: ?Sized,
Self::Item: SafeCastable<T>,
{...}
fn try_cast<T>(self) -> Try<Self::SmartPtr<T>, <Self::Item as SafeTryCastable<T>>::R::Residual>
where
T: ?Sized,
Self::Item: SafeTryCastable<T>,
{...}
}
Related: rust-lang/rust#44874
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 30 (15 by maintainers)
You can use
rustc_allow_incoherent_implto add those inherent functions in std.I think this should be reopened because the problem remains and libs-api has glossed over the entire “the Orphan Rule is a thing” in closing this.
Or, we could (secretly) promote those types to
core::ffi/alloc::ffi, and the coherence problems will disappear.Looks good to me. Seconded, assuming no objections from other team members.
I think josh is talking about
not the higher-kinded generic alternative.
Yes, the Wtf8 code has to move to core.
Generally you’d give it a feature name with “internal” in there somewhere and not provide an issue number. Like this:
https://github.com/rust-lang/rust/blob/334e509912d82323ff0fca016a2e69dbbee559fd/library/core/src/panicking.rs#L25
Actually, adding
#[fundamental]doesn’t even get pastallocitself:… although I don’t see how those downstream impls would be allowed at all. 😕
edit: oh, that’s probably for downstream
A.#[fundamental]changes what coherence considers covered, which can break downstream code, e.g.:playground
So that
Boximpl isn’t allowed, butRcandArccurrently are – and those would break too if made fundamental.It’s a silly example, and I have no idea if real world code currently relies on this coherence coverage, but we could use crater to find out. I think this would also need T-lang signoff, not just libs.
I think the
Boximpls are not prevented by orphan rules sinceBoxis#[fundamental]?RcandArcdo have problems though.Also: There’s already an
impl From<String> for OsString, but there’s noimpl TryFrom<OsString> for String(though there’s ato_strmethod). There was some interest in the meeting in seeing that added as well.We discussed this in today’s meeting. We’re approving this, but since it’s insta-stable, the PR will need to be FCPed.
What’s the reason to make these all dedicated methods, rather than implementing
TryFromfor the fallible conversions andFromfor the infallible ones?