-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Implement Rc
/Arc
conversions for string-like types
#45990
Conversation
src/libstd/ffi/c_str.rs
Outdated
#[inline] | ||
fn from(s: CString) -> Arc<CStr> { | ||
let arc: Arc<[u8]> = Arc::from(s.into_inner()); | ||
unsafe { mem::transmute(arc) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we do this via Arc::into_raw
+ x as *const CStr
+ Arc::from_raw
instead of transmute?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I've now replaced each instance of transmute.
Tests passed. r? @kennytm |
r? @BurntSushi |
#[inline] | ||
fn from(s: &CStr) -> Arc<CStr> { | ||
let arc: Arc<[u8]> = Arc::from(s.to_bytes_with_nul()); | ||
unsafe { Arc::from_raw(Arc::into_raw(arc) as *const CStr) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this safe? What guarantees that &'a CStr
outlives the resulting Arc<CStr>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would &'a CStr
need to outlive Arc<CStr>
? Arc::from
does a new heap allocation and copies over all the contents of s
.
The unsafe
block just does Arc<[u8]> -> Arc<CStr>
. This is safe since the invariants of CStr
are maintained.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see. I didn't realize there was a copy here. In hindsight, the Arc<[u8]>
type should have told me that. And of course this impl: https://doc.rust-lang.org/std/sync/struct.Arc.html#impl-From<&'a [T]>
This is continuation of |
This seems reasonable to me! @rfcbot merge |
@BurntSushi The correct command is |
Let's try it again... @rfcbot fcp merge |
Team member @BurntSushi has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Ping @Kimundi, ticky boxes for you! |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
@bors: r+ |
📌 Commit 3c1d59f has been approved by |
⌛ Testing commit 3c1d59f61817c4470ebbaa17c925633c71cb1fd3 with merge b199f97bdde51ed7ad4c16799c3ef05a354428e1... |
💔 Test failed - status-appveyor |
Could not compile stage0-libstd on all Windows, missing imports.
|
@kennytm: Fixed. re-r+? |
@bors r=alexcrichton |
📌 Commit 85e6421 has been approved by |
Implement `Rc`/`Arc` conversions for string-like types Provides the following conversion implementations: * `From<`{`CString`,`&CStr`}`>` for {`Arc`,`Rc`}`<CStr>` * `From<`{`OsString`,`&OsStr`}`>` for {`Arc`,`Rc`}`<OsStr>` * `From<`{`PathBuf`,`&Path`}`>` for {`Arc`,`Rc`}`<Path>` Closes rust-lang#45008
⌛ Testing commit 85e6421 with merge 5600b272ddaffa84cdbbdf4872a89a2680d72ccf... |
💔 Test failed - status-travis |
Provides the following conversion implementations: * `From<`{`CString`,`&CStr`}`>` for {`Arc`,`Rc`}`<CStr>` * `From<`{`OsString`,`&OsStr`}`>` for {`Arc`,`Rc`}`<OsStr>` * `From<`{`PathBuf`,`&Path`}`>` for {`Arc`,`Rc`}`<Path>`
Psh. The wasm stuff merged and added a new |
@bors r=alexcrichton |
📌 Commit 1bbc776 has been approved by |
Implement `Rc`/`Arc` conversions for string-like types Provides the following conversion implementations: * `From<`{`CString`,`&CStr`}`>` for {`Arc`,`Rc`}`<CStr>` * `From<`{`OsString`,`&OsStr`}`>` for {`Arc`,`Rc`}`<OsStr>` * `From<`{`PathBuf`,`&Path`}`>` for {`Arc`,`Rc`}`<Path>` Closes #45008
☀️ Test successful - status-appveyor, status-travis |
Correct a few stability attributes * The extra impls for `ManuallyDrop` were added in rust-lang#44310 which was only stabilised in 1.22.0. * The impls for `SliceIndex` were stabilised in rust-lang#43373 but as `RangeInclusive` and `RangeToInclusive` are still unstable the impls should remain unstable. * The `From` impls for atomic integers were added in rust-lang#45610 but most atomic integers are still unstable. * The `shared_from_slice2` impls were added in rust-lang#45990 but they won't be stable until 1.24.0. * The `Mutex` and `RwLock` impls were added in rust-lang#46082 but won't be stable until 1.24.0.
Correct a few stability attributes * The extra impls for `ManuallyDrop` were added in rust-lang#44310 which was only stabilised in 1.22.0. * The impls for `SliceIndex` were stabilised in rust-lang#43373 but as `RangeInclusive` and `RangeToInclusive` are still unstable the impls should remain unstable. * The `From` impls for atomic integers were added in rust-lang#45610 but most atomic integers are still unstable. * The `shared_from_slice2` impls were added in rust-lang#45990 but they won't be stable until 1.24.0. * The `Mutex` and `RwLock` impls were added in rust-lang#46082 but won't be stable until 1.24.0.
Correct a few stability attributes * The extra impls for `ManuallyDrop` were added in rust-lang#44310 which was only stabilised in 1.22.0. * The impls for `SliceIndex` were stabilised in rust-lang#43373 but as `RangeInclusive` and `RangeToInclusive` are still unstable the impls should remain unstable. * The `From` impls for atomic integers were added in rust-lang#45610 but most atomic integers are still unstable. * The `shared_from_slice2` impls were added in rust-lang#45990 but they won't be stable until 1.24.0. * The `Mutex` and `RwLock` impls were added in rust-lang#46082 but won't be stable until 1.24.0.
Provides the following conversion implementations:
From<
{CString
,&CStr
}>
for {Arc
,Rc
}<CStr>
From<
{OsString
,&OsStr
}>
for {Arc
,Rc
}<OsStr>
From<
{PathBuf
,&Path
}>
for {Arc
,Rc
}<Path>
Closes #45008