Skip to content
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

Merged
merged 1 commit into from
Nov 26, 2017
Merged

Implement Rc/Arc conversions for string-like types #45990

merged 1 commit into from
Nov 26, 2017

Conversation

murarth
Copy link
Contributor

@murarth murarth commented Nov 14, 2017

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

@kennytm kennytm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. relnotes Marks issues that should be documented in the release notes of the next release. labels Nov 14, 2017
#[inline]
fn from(s: CString) -> Arc<CStr> {
let arc: Arc<[u8]> = Arc::from(s.into_inner());
unsafe { mem::transmute(arc) }
Copy link
Member

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?

Copy link
Contributor Author

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.

@murarth
Copy link
Contributor Author

murarth commented Nov 14, 2017

Tests passed. r? @kennytm

@kennytm
Copy link
Member

kennytm commented Nov 15, 2017

r? @BurntSushi

@rust-highfive rust-highfive assigned BurntSushi and unassigned kennytm Nov 15, 2017
#[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) }
Copy link
Member

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>?

Copy link
Contributor

@Centril Centril Nov 15, 2017

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.

Copy link
Member

@BurntSushi BurntSushi Nov 15, 2017

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]>

@BurntSushi BurntSushi added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 15, 2017
@Centril
Copy link
Contributor

Centril commented Nov 15, 2017

This is continuation of RFC 1845, shared_from_slice, which motivates this PR =)

@BurntSushi
Copy link
Member

This seems reasonable to me!

@rfcbot merge

@kennytm kennytm added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 15, 2017
@kennytm
Copy link
Member

kennytm commented Nov 15, 2017

@BurntSushi The correct command is @rfcbot fcp merge...

@BurntSushi
Copy link
Member

Let's try it again...

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Nov 15, 2017

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.

@rfcbot rfcbot added the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Nov 15, 2017
@kennytm
Copy link
Member

kennytm commented Nov 22, 2017

Ping @Kimundi, ticky boxes for you!

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Nov 22, 2017
@rfcbot
Copy link

rfcbot commented Nov 22, 2017

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Nov 22, 2017
@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Contributor

bors commented Nov 25, 2017

📌 Commit 3c1d59f has been approved by alexcrichton

@kennytm kennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Nov 25, 2017
@bors
Copy link
Contributor

bors commented Nov 25, 2017

⌛ Testing commit 3c1d59f61817c4470ebbaa17c925633c71cb1fd3 with merge b199f97bdde51ed7ad4c16799c3ef05a354428e1...

@bors
Copy link
Contributor

bors commented Nov 25, 2017

💔 Test failed - status-appveyor

@kennytm
Copy link
Member

kennytm commented Nov 25, 2017

Could not compile stage0-libstd on all Windows, missing imports.
error[E0433]: failed to resolve. Use of undeclared type or module `Arc`
   --> src\libstd\sys\windows\os_str.rs:162:18
    |
162 |         unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Slice) }
    |                  ^^^ Use of undeclared type or module `Arc`
error[E0433]: failed to resolve. Use of undeclared type or module `Arc`
   --> src\libstd\sys\windows\os_str.rs:162:32
    |
162 |         unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Slice) }
    |                                ^^^ Use of undeclared type or module `Arc`
error[E0412]: cannot find type `Arc` in this scope
   --> src\libstd\sys\windows\os_str.rs:121:31
    |
121 |     pub fn into_arc(&self) -> Arc<Slice> {
    |                               ^^^ not found in this scope
    |
help: possible candidate is found in another module, you can import it into scope
    |
14  | use alloc::arc::Arc;
    |
error[E0412]: cannot find type `Arc` in this scope
   --> src\libstd\sys\windows\os_str.rs:160:31
    |
160 |     pub fn into_arc(&self) -> Arc<Slice> {
    |                               ^^^ not found in this scope
    |
help: possible candidate is found in another module, you can import it into scope
    |
14  | use alloc::arc::Arc;
    |
error: aborting due to 4 previous errors
error: Could not compile `std`.

@kennytm kennytm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 25, 2017
@murarth
Copy link
Contributor Author

murarth commented Nov 25, 2017

@kennytm: Fixed. re-r+?

@kennytm
Copy link
Member

kennytm commented Nov 25, 2017

@bors r=alexcrichton

@bors
Copy link
Contributor

bors commented Nov 25, 2017

📌 Commit 85e6421 has been approved by alexcrichton

@kennytm kennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 25, 2017
kennytm added a commit to kennytm/rust that referenced this pull request Nov 25, 2017
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
@bors
Copy link
Contributor

bors commented Nov 26, 2017

⌛ Testing commit 85e6421 with merge 5600b272ddaffa84cdbbdf4872a89a2680d72ccf...

@bors
Copy link
Contributor

bors commented Nov 26, 2017

💔 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>`
@murarth
Copy link
Contributor Author

murarth commented Nov 26, 2017

Psh. The wasm stuff merged and added a new OsString implementation. It's a straight copy of the Unix implementation, so I copied over the changes.

@kennytm
Copy link
Member

kennytm commented Nov 26, 2017

@bors r=alexcrichton

@bors
Copy link
Contributor

bors commented Nov 26, 2017

📌 Commit 1bbc776 has been approved by alexcrichton

@bors
Copy link
Contributor

bors commented Nov 26, 2017

⌛ Testing commit 1bbc776 with merge d4dc289...

bors added a commit that referenced this pull request Nov 26, 2017
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
@bors
Copy link
Contributor

bors commented Nov 26, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: alexcrichton
Pushing d4dc289 to master...

@bors bors merged commit 1bbc776 into rust-lang:master Nov 26, 2017
@murarth murarth deleted the rc-from-strs branch November 26, 2017 17:13
lo48576 pushed a commit to lo48576/opaque_typedef that referenced this pull request Dec 2, 2017
steveklabnik added a commit to steveklabnik/rust that referenced this pull request Jan 4, 2018
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.
kennytm added a commit to kennytm/rust that referenced this pull request Jan 4, 2018
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.
kennytm added a commit to kennytm/rust that referenced this pull request Jan 5, 2018
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants