-
Notifications
You must be signed in to change notification settings - Fork 20
New failable API for SmartPointer<OsStr> to SmartPointer<str>, etc #399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What's the reason to make these all dedicated methods, rather than implementing |
i tried it and figured out that rust's generic system is a bit too limited for this To implement TryFrom/From for any smart pointer, you'd need a |
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. |
Thanks @kennytm ! cc @joshtriplett I've added the |
Looks good to me. Seconded, assuming no objections from other team members. |
We discussed this in today's meeting. We're approving this, but since it's insta-stable, the PR will need to be FCPed. |
Also: There's already an |
@joshtriplett I'm working on it , however the orphan rules prevents me from doing it since Arc/Rc/Box is defined in alloc, not in std, and Is there anyway I can workaround that? Or maybe we shall have a different API? |
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. |
Agreed, it's not clear how to implement it given the orphan rule constraint. TBF I kind of think that core/alloc/std should be considered as one crate, from outside of the stdlib it really works like one crate. core and alloc exists merely because rust needs to support no-std, core and alloc is effectively a subset of stdlib in terms of functionality. |
I think the
|
It's not immediately obvious to me why Arc and Rc aren't fundamental. That aside, I'm surprised if this can't be defined in any of the three crates, since it seems like it should be definable in a crate introducing one of the types... |
But yeah I can already implement conversion for |
cc @joshtriplett shall I open a PR to set Arc and Rc as fundamental? |
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!()
}
}
So that 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. |
Actually, adding
... although I don't see how those downstream impls would be allowed at all. :/ edit: oh, that's probably for downstream |
I tried adding a negative-impl |
@cuviper is there anyway of implementing |
Not in |
Or, we could (secretly) promote those types to |
Thanks @cuviper , I could open a PR to promote those types to core/alloc. It doesn't look like they use any syscall, only platform-dependent parsing, so it should be possible. |
@cuviper Just realize that So I can move One way to fix it would be to put them in a sealed traits implemented in std, but the trait has to be imported for it to work, otherwise it'd break backwards compatibility. I suppose that importing the trait in prelude could maintain that backwards compatibility, but I'm not sure if it possible to opt-out of importing prelude. If it is possible, then this won't work. |
You can use |
Thanks, @pitaj that's exactly the magic token I need for. |
Is there anyway to mark a type/API as permanently unstable? I need to move the However since it won't be stablised, I'd need to mark it as permanent unstable for now (if someone wants it stablised, they can open a separate issue here in this repo). |
Generally you'd give it a feature name with "internal" in there somewhere and not provide an issue number. Like this: |
Thank you! |
You can make them FWIW, I would move as little as you can get away with, keeping most of the impls in std if possible. But I guess trait impls will have to move for the same reason you're trying this in the first place. |
Well And yeah the trait impls should be moved as well, oitherwise leaving it half done doesn't make sense.. |
Yes, the Wtf8 code has to move to core. |
Proposal
Problem statement
cc uses
Arc<OsStr>
a lot (for env and flags) and we sometimes need them converted to a string.Since these
Arc
s are wrapped with lock, it can't return&str
but would have to convert them toString
.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 convertArc<OsStr>
toArc<str>
in a faillable way.Solution sketch
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:
Related: rust-lang/rust#44874
The text was updated successfully, but these errors were encountered: