reduce temporary allocations in Color serde impl #925
+40
−60
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
hello. i have made some small changes in the impl of Serialize and Deserialize for
crossterm::style::Color
which reduces the number of temporary allocations created. To achieve this, I have:str::replace
with other suitable methods such asstr::strip_{suffix|prefix}
in the deserialize implformat!
macroFor ease of implementation, I have brought in the
arrayvec
dependency, dependent on theserde
crossterm feature. This enables using thewrite!
macro on anarrayvec::ArrayString
rather than fiddling with an array on the stack and having to uphold utf8 invariants manually. The length of the stack-based string is based on the longest possible formatted color, which is"rgb_(255,255,255)"
, at length 17.The deserialize tests as written are all still passing, and a manual check of the serialize impls confirms that colors
Color::Rgb{ r: 255, g: 255, b: 255 }
,Color::Ansi(1)
, andColor::Red
still serialize as expected (debug printing shown):