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

Remove impl Deref for AnsiGenericString #5

Merged
merged 3 commits into from
Jun 3, 2022

Conversation

sholderbach
Copy link
Member

The Deref implementation for AnsiGenericString and its derived type aliases probably violates the tacit definition of the Deref trait:

https://doc.rust-lang.org/std/ops/trait.Deref.html

As the type aliases implement Display for the actual styling logic they get a blanket impl ToString for T: Display + ?Sized

This has the nasty consequence that while you would expect that fn foo(input: AsRef<str>) would get the same input from let styled: AnsiGenericString<_> = ... for both foo(styled) and foo(styled.to_string()) this doesn't hold.

Thus the lint https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned will change the actual behavior and remove styling as the underlying string gets presented via Deref.

This change removes the Deref implementation and fixes the internal uses.

The `Deref` implementation for `AnsiGenericString` and its derived type
aliases probably violates the tacit definition of the `Deref` trait"

https://doc.rust-lang.org/std/ops/trait.Deref.html

As the type aliases implement `Display` for the actual styling logic
they get a blanket `impl ToString for T: Display + ?Sized`

This has the nasty consequence that while you would expect that
`fn foo(input: AsRef<str>)` would get the same input from `let styled:
AnsiGenericString<_> = ...` for both `foo(styled)` and
`foo(styled.to_string())` this doesn't hold.

Thus the lint
https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned
will change the actual behavior and remove styling as the underlying
string gets presented via `Deref`.

This change removes the `Deref` implementation and fixes the internal
uses.
@sholderbach sholderbach changed the title remove deref Remove impl Deref for AnsiGenericString May 30, 2022
Breaking change: remove deref implementation on the styled output
`AnsiGenericString`
@sholderbach sholderbach marked this pull request as ready for review June 3, 2022 11:07
@sholderbach
Copy link
Member Author

Tested with nushell and reedline. Doesn't affect tests, builds pass, visual appearance is maintained.

@sholderbach sholderbach merged commit 4a7b96c into nushell:main Jun 3, 2022
sholderbach added a commit to nushell/nushell that referenced this pull request Jun 3, 2022
Resolves an unexpected issue due to `Deref` and `ToString` interacting

Details: nushell/nu-ansi-term#5 and nushell/reedline#435 (comment)

Also updates reedline: Includes a fix for a panic when the directory containing the history is deleted during a running reedline session. (nushell/reedline#436)
fennewald pushed a commit to fennewald/nushell that referenced this pull request Jun 27, 2022
Resolves an unexpected issue due to `Deref` and `ToString` interacting

Details: nushell/nu-ansi-term#5 and nushell/reedline#435 (comment)

Also updates reedline: Includes a fix for a panic when the directory containing the history is deleted during a running reedline session. (nushell/reedline#436)
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.

1 participant