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

Add a conversion from &str to CompleteStr #725

Merged
merged 2 commits into from
Apr 9, 2018

Conversation

myrrlyn
Copy link
Contributor

@myrrlyn myrrlyn commented Mar 28, 2018

This commit expands on the addition of Deref<Target=&str> and provides a means of downcasting an &str into a CompleteStr.

This should not be used arbitrarily, but is an excellent convenience for manipulating CompleteStrs using &str methods. Since the Deref work always returns bare &strs, even from string slices that are known to be CompleteStr, converting back into CompleteStr is safe and useful.

myrrlyn added 2 commits March 28, 2018 11:21
This change is necessary because of how Rust tracks lifetimes and
reference collapse. The Deref trait returns a reference to the interior
of a Complete struct, and does NOT copy out the interior reference. This
means that anything consuming the reference coming out of Deref::deref
loses awareness of the lifetime of the /referent/, and only knows the
lifetime of the Complete struct, which is practically always the local
scope.

Furthermore, a From impl is added to take an &'anon &'a Inner and build
a fresh Complete type with the correct lifetime parameter, NOT the 'anon
lifetime generated by Deref::deref.

As a result of this change, the following formerly-invalid code is now
valid:

```rust
let x: Complete;
let y: Complete = x.deref().into();
```

The second Complete will have the same lifetime parameter as the first,
and will not have the lifetime parameter of the first's *scope*.
@myrrlyn
Copy link
Contributor Author

myrrlyn commented Apr 4, 2018

Despite the title, this PR operates on both CompleteStr and CompleteByteSlice.

fn deref(&self) -> &str {
self.0
fn deref(&self) -> &Self::Target {
&self.0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The value returned from Deref::deref is now an &'_ &'a str, suitable for reconstruction into a CompleteStr<'a> via the From impl above. Formerly, the value returned was an &'_ str where the reference lifetime was tied to the newtype wrapper, not to the referent str, which was impossible to unify during From.

@Geal Geal merged commit 605396f into rust-bakery:master Apr 9, 2018
@Geal
Copy link
Collaborator

Geal commented Apr 9, 2018

thanks, this looks good :)

@myrrlyn myrrlyn deleted the feature/completestr-from-str branch April 25, 2018 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants