From bd5fa963ff4953a07d423b0ebf7d232cfaaee4d0 Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Sun, 15 Sep 2024 20:29:04 -0700 Subject: [PATCH] feat(line)!: impl From> for Line BREAKING-CHANGES: `Line` now implements `From` As this adds an extra conversion, ambiguous inferred values may no longer compile. ```rust // given: struct Foo { ... } impl From for String { ... } impl From for Cow { ... } let foo = Foo { ... }; let line = Line::from(foo); // now fails due to ambiguous type inference // replace with let line = Line::from(String::from(foo)); ``` Fixes: https://github.com/ratatui/ratatui/issues/1367 --- BREAKING-CHANGES.md | 22 ++++++++++++++++++++-- src/text/line.rs | 6 ++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 012a5592..12a9052d 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -65,6 +65,26 @@ This is a quick summary of the sections below: - MSRV is now 1.63.0 - `List` no longer ignores empty strings +## v0.29.0 (Unreleased) + +### `Line` now implements `From` ([#1373]) + +[#1373]: https://github.com/ratatui/ratatui/pull/1373 + +As this adds an extra conversion, ambiguous inferred values may no longer compile. + +```rust +// given: +struct Foo { ... } +impl From for String { ... } +impl From for Cow { ... } + +let foo = Foo { ... }; +let line = Line::from(foo); // now fails due to now ambiguous inferred type +// replace with +let line = Line::from(String::from(foo)); +``` + ## v0.28.0 ### `Backend::size` returns `Size` instead of `Rect` ([#1254]) @@ -136,8 +156,6 @@ This change simplifies the trait and makes it easier to implement. ### `Frame::size` is deprecated and renamed to `Frame::area` -[#1293]: https://github.com/ratatui/ratatui/pull/1293 - `Frame::size` is renamed to `Frame::area` as it's the more correct name. ## [v0.27.0](https://github.com/ratatui/ratatui/releases/tag/v0.27.0) diff --git a/src/text/line.rs b/src/text/line.rs index 3082635d..93029d1f 100644 --- a/src/text/line.rs +++ b/src/text/line.rs @@ -515,6 +515,12 @@ impl<'a> From<&'a str> for Line<'a> { } } +impl<'a> From> for Line<'a> { + fn from(s: Cow<'a, str>) -> Self { + Self::raw(s) + } +} + impl<'a> From>> for Line<'a> { fn from(spans: Vec>) -> Self { Self {