Skip to content

Implement TryFrom for CString and CStr - #44916

Closed
nvzqz wants to merge 1 commit into
rust-lang:masterfrom
nvzqz:str-try-from
Closed

Implement TryFrom for CString and CStr#44916
nvzqz wants to merge 1 commit into
rust-lang:masterfrom
nvzqz:str-try-from

Conversation

@nvzqz

@nvzqznvzqz commented Sep 29, 2017

Copy link
Copy Markdown
Contributor

Implements TryFrom by utilizing the available conversions.

This is a subset of #33417.

Conversions

FromIntoKind
&{mut,} [u8]&{mut,} strUTF-8
Vec<u8>StringUTF-8
&[u16]StringUTF-16
&[u8]&CStrUTF-8 with nul
CStringStringUTF-8
Into<Vec<u8>>CStringUTF-8 with nul

Motivation

These types already have conversions that return Result<Self, Error>. This simply implements TryFrom for such types.

@rust-highfive

Copy link
Copy Markdown
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @BurntSushi (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@nvzqz

Copy link
Copy Markdown
ContributorAuthor

@sfackler would you like to take a look at this one? It's similar to #44764 in nature.

@nvzqznvzqz changed the title Implement TryFrom<&[u8]> for &strImplement TryFrom for String TypesSep 29, 2017
@nvzqz

Copy link
Copy Markdown
ContributorAuthor

r? @sfackler

@nvzqz

nvzqz commented Sep 29, 2017

Copy link
Copy Markdown
ContributorAuthor

Getting this error now:

[00:02:28] error: use of unstable library feature 'try_from' (see issue #33417)
[00:02:28] --> /checkout/src/liballoc/string.rs:60:5
[00:02:28] |
[00:02:28] 60 | use core::convert::TryFrom;
[00:02:28] | ^^^^^^^^^^^^^^^^^^^^^^
[00:02:28] |
[00:02:28] = help: add #![feature(try_from)] to the crate attributes to enable
[00:02:28] [00:02:28] error: use of unstable library feature 'try_from' (see issue #33417)
[00:02:28] --> /checkout/src/liballoc/string.rs:1607:5
[00:02:28] |
[00:02:28] 1607 | type Error = FromUtf8Error;
[00:02:28] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
[00:02:28] |
[00:02:28] = help: add #![feature(try_from)] to the crate attributes to enable

Even though I marked the impl as unstable.

Seems to be fixed with 526de507c81062905e2206eac06308cfe63a91a3.

@arthurprs

Copy link
Copy Markdown
Contributor

Thank you. Very useful 😄

@shepmastershepmaster added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 29, 2017
@sfackler

Copy link
Copy Markdown
Member

I'm feeling a bit unsure about the UTF8 and UTF16 conversions - for [u8] in particular, there are all kinds of text encodings, and it's not necessarily clear that it would obviously interpret it as UTF8 rather than trying to infer the type or whatever.

@sfacklersfackler added the T-libs-api [DEPRECATED; DO NOT USE] label Sep 29, 2017
@sfackler

Copy link
Copy Markdown
Member

cc @rust-lang/libs

@aidanhsaidanhs added S-waiting-on-team and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 5, 2017
@bors

bors commented Oct 13, 2017

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (presumably #45233) made this pull request unmergeable. Please resolve the merge conflicts.

@shepmaster

Copy link
Copy Markdown
Member

Yo — @rust-lang/libs — it's been 16 days since we heard from all y'all. Care to chime in?

@sfacklersfackler added the I-needs-decision Issue: In need of a decision. label Oct 15, 2017
@alexcrichton

Copy link
Copy Markdown
Member

These seem fine to me, @sfackler I don't have too many thoughts about the encoding question, it seems fine to me to take a conservative route and not add them for now.

@sfackler

Copy link
Copy Markdown
Member

That'd limit it to the cstr impls then.

@kennytm

Copy link
Copy Markdown
Member

Hello @nvzqz @rust-lang/libs — Is there a decision for this PR, that we limit to the CStr implementations?:

// keep these:impl<'a>TryFrom<&'a[u8]>for&'aCStr{}implTryFrom<Vec<u8>>forCString{}implTryFrom<CString>forString{}// and remove the rest?

@nvzqz

nvzqz commented Nov 2, 2017

Copy link
Copy Markdown
ContributorAuthor

Makes sense to me. I'll make the appropriate changes later today.

@carols10centscarols10cents 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-team labels Nov 6, 2017
@shepmaster

Copy link
Copy Markdown
Member

I'll make the appropriate changes later today.

Hello from triage, @nvzqz! It's been a week since we last heard from you and it appears you've got some merge conflicts! Will you have some time to address those and the most recent feedback you received?

@nvzqz

Copy link
Copy Markdown
ContributorAuthor

@shepmaster just addressed these changes. Thanks for the reminder 😄

Comment threadsrc/libstd/ffi/c_str.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks for valid UTF-8 so this impl should also be removed.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryFrom<CString> for String is an obvious conversion. The underlying conversion checks valid UTF-8 but CString is a named and obvious type unlike Vec<u8>.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A CString is a sequence of none nul bytes. A Vec<u8> is a sequence of bytes. The conversion to String is exactly the same so it doesn't make sense to include one but not the other.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kennytm@sfackler do you guys have any input on this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd lean towards leaving this out for now.

@kennytmkennytm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI failed...

Comment threadsrc/libstd/ffi/c_str.rs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[00:02:22] error[E0119]: conflicting implementations of trait `core::convert::TryFrom<_>` for type `ffi::c_str::CString`:
[00:02:22] --> /checkout/src/libstd/ffi/c_str.rs:651:1
[00:02:22] |
[00:02:22] 651 | / impl<T: Into<Vec<u8>>> TryFrom<T> for CString {
[00:02:22] 652 | | type Error = NulError;
[00:02:22] 653 | |
[00:02:22] 654 | | fn try_from(t: T) -> Result<CString, NulError> {
[00:02:22] 655 | | CString::new(t)
[00:02:22] 656 | | }
[00:02:22] 657 | | }
[00:02:22] | |_^
[00:02:22] |
[00:02:22] = note: conflicting implementation in crate `core`

It is conflicting with impl<T, U> TryFrom<U> for T where T: From<U>, because CString: Into<Vec<u8>>, i.e. this overlaps with the identity transformation CString: TryFrom<CString>.

Try to change it to impl TryFrom<Vec<u8>> for CString and impl<'a> TryFrom<&'a [u8]> for CString.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should just be TryFrom<Vec<u8>> instead. Only having the slice conversion results in a potentially unwanted allocation in the case of:

let vec = vec![/* ... */];let c_str:CString = vec.try_into()?;

@kennytm

Copy link
Copy Markdown
Member

Hi @nvzqz, do you have time to fix the CI failure?

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

Copy link
Copy Markdown
Member

Ok @sfackler it's your turn again.

BTW @nvzqz I strongly suggest you rebase and combine everything down to a single commit.

@nvzqznvzqz changed the title Implement TryFrom for String TypesImplement TryFrom for CString and CStrNov 23, 2017
Conversions:
- Vec<u8> to CString
- CString to String
- &[u8] to &CStr
@carols10cents

Copy link
Copy Markdown
Member

Review ping for you @sfackler !

@kennytm

Copy link
Copy Markdown
Member

@sfackler There is a question for you too on #44916 (review).

@kennytmkennytm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed I-needs-decision Issue: In need of a decision. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 29, 2017
@kennytm

Copy link
Copy Markdown
Member

@nvzqz I think the consensus here is to keep only the encoding-agnostic conversions [u8]CStr and Vec<u8>CString.

@ollie27

Copy link
Copy Markdown
Contributor

There is also an inconsistency between the &[u8]&CStr and Vec<u8>CString conversions. &[u8]&CStr requires the slice ends in a nul byte but Vec<u8>CString doesn't. At the very least that needs to be documented on the impls.

@kennytmkennytm added S-waiting-on-team and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 6, 2017
@kennytm

Copy link
Copy Markdown
Member

I'm nominating this PR to the @rust-lang/libs team. The consensus so far is removing every conversion involving string encodings, which leaves us with [u8]CStr and Vec<u8>CString.

However, as #44916 (comment) pointed out, these two conversions are semantically incompatible in this current form. I can see several resolutions:

  1. Accept the discrepancy, improve documentation
  2. Remove [u8]CStr and keep only Vec<u8>CString
  3. Remove Vec<u8>CString and keep only [u8]CStr
  4. Keep [u8]CStr and change Vec<u8>CString to require a trailing zero
  5. Give up, don't add any TryFrom implementations.
  6. Some other creative solution?

No matter which is picked, I don't think the author can proceed without further decisions.

@sfackler

sfackler commented Dec 6, 2017

Copy link
Copy Markdown
Member

The fact that there is any debate over what the semantics of these impls may indicate we shouldn't add them.

@kennytm

Copy link
Copy Markdown
Member

Triage ping @rust-lang/libs @sfackler! Any update on this PR?

@dtolnaydtolnay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed these impls with the libs team. Regarding &[u8] ⟶ &CStr and Vec<u8> ⟶ CString we agreed on option 5 of Kenny's #44916 (comment) -- give up and don't add these as TryFrom implementations. We prefer for code to use the verbose but unambiguous named constructors like CStr::from_bytes_with_nul if there is any question of what the impl does.

Feeling the need to document what the impl does is a sure sign that there is a question of what the impl does. From and TryFrom impls should never need explanatory documentation.

The &[u8] ⟶ &CStr conversion is the one that comes closest to meeting this bar. If someone understand the invariants of CStr and the implications of & ⟶ &, it really narrows it down to one possible thing the TryFrom impl could reasonably do. But even for that we felt it involves too much background knowledge and in practice the impl would need to be documented, so the conversion should not be handled by a TryFrom impl.

About CString ⟶ String we felt less strongly, but from reading the discussion here again I agree with the consensus of ruling this one out for the same reason as Vec<u8> ⟶ String.

As a secondary concern, we were skeptical about the motivation for adding any of these TryFrom impls. In general we felt that the existence of a Result<Self, Error> method does not necessary mean that an equivalent TryFrom impl needs to exist. We would have preferred for this to be motivated by a concrete API that someone wanted to write, some function where it makes sense to take T: TryInto<&'a CStr> that cannot be expressed any other way that is as ergonomic for callers.

Thanks anyway for the PR and for the insightful discussion -- this is what helps figure out the role we want for TryFrom.

@dtolnaydtolnay closed this Dec 21, 2017
@nvzqz
nvzqz deleted the str-try-from branch December 25, 2017 19:37
@dtolnaydtolnay self-assigned this Mar 24, 2024
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-libs-api[DEPRECATED; DO NOT USE]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

13 participants

@nvzqz@rust-highfive@arthurprs@sfackler@bors@shepmaster@alexcrichton@kennytm@carols10cents@ollie27@dtolnay@BurntSushi@aidanhs