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)

Most upvoted comments

You can use rustc_allow_incoherent_impl to 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

impl TryFrom<Arc<OsStr>> for Arc<str> {}
impl TryFrom<Rc<OsStr>> for Rc<str> {}
impl TryFrom<Box<OsStr>> for Box<str> {}
impl From<Arc<str>> for Arc<OsStr> {}
impl From<Rc<str>> for Rc<OsStr> {}
impl From<Box<str>> for Box<OsStr> {}

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 past alloc itself:

error[E0119]: conflicting implementations of trait `TryFrom<Rc<[_], _>>` for type `Rc<[_; _], _>`
    --> alloc/src/rc.rs:2745:1
     |
2745 | impl<T, A: Allocator, const N: usize> TryFrom<Rc<[T], A>> for Rc<[T; N], A> {
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |
     = note: conflicting implementation in crate `core`:
             - impl<T, U> TryFrom<U> for T
               where U: Into<T>;
     = note: downstream crates may implement trait `core::convert::From<rc::Rc<[_], _>>` for type `rc::Rc<[_; _], _>`

error[E0119]: conflicting implementations of trait `TryFrom<Arc<[_], _>>` for type `Arc<[_; _], _>`
    --> alloc/src/sync.rs:3703:1
     |
3703 | impl<T, A: Allocator, const N: usize> TryFrom<Arc<[T], A>> for Arc<[T; N], A> {
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |
     = note: conflicting implementation in crate `core`:
             - impl<T, U> TryFrom<U> for T
               where U: Into<T>;
     = note: downstream crates may implement trait `core::convert::From<sync::Arc<[_], _>>` for type `sync::Arc<[_; _], _>`

… 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.:

pub struct Foo(pub [u8]);

impl<T> AsRef<Foo> for std::sync::Arc<T> {
    fn as_ref(&self) -> &Foo {
        todo!()
    }
}

impl<T> AsRef<Foo> for std::rc::Rc<T> {
    fn as_ref(&self) -> &Foo {
        todo!()
    }
}

impl<T> AsRef<Foo> for Box<T> {
    fn as_ref(&self) -> &Foo {
        todo!()
    }
}

playground

error[E0210]: type parameter `T` must be covered by another type when it appears before the first local type (`Foo`)
  --> src/lib.rs:15:6
   |
15 | impl<T> AsRef<Foo> for Box<T> {
   |      ^ type parameter `T` must be covered by another type when it appears before the first local type (`Foo`)
   |
   = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local, and no uncovered type parameters appear before that first local type
   = note: in this case, 'before' refers to the following order: `impl<..> ForeignTrait<T1, ..., Tn> for T0`, where `T0` is the first and `Tn` is the last

For more information about this error, try `rustc --explain E0210`.

So that Box impl isn’t allowed, but Rc and Arc currently 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 Box impls are not prevented by orphan rules since Box is #[fundamental]?

Rc and Arc do have problems though.

Also: There’s already an impl From<String> for OsString, but there’s no impl TryFrom<OsString> for String (though there’s a to_str method). 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 TryFrom for the fallible conversions and From for the infallible ones?