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.
Currently, there exist the borrowing
Attrs
and the owningAttrsOwned
, which only differ in their usage ofFamily
vsFamilyOwned
. In theory, this allows passingAttrs
without having to allocate. In practice, we have the following situation:FontSystem::get_font_matches
wants to prevent an allocation in the most-common case that a cached match already exists, but it can't (seecosmic-text/src/font/system/std.rs
Line 130 in 1bc198f
Borrow<Attrs>
forAttrsOwned
.AttrsOwned
inAttrsList
, so it is almost impossible to not have anAttrs
turn into anAttrsOwned
somewhere down the line.This PR removes the old
Attrs
, renamesAttrsOwned
toAttrs
and adds anAttrsBuilder
, which has the same helper methods as the oldAttrs
. This PR also includes several changes to prevent allocations in many cases:FamilyOwned
uses aCow<'static, str>
instead ofString
, allowing the use of string literals without allocating.AttrsBuilder::family
takes animpl Into<FamilyOwned>
, which is only implemented forFamilyOwned
orFamily<'static>
, so users need to explicity useFamilyOwned::new
if they want to use a non staticFamily
.Attrs
implementsAsRef<Attrs>
andFrom<&Attrs>
, allowing methods that might need to store theAttrs
to accept both values and references ofAttrs
, while at the same time preventing an extra allocation when a value is passed.Attrs
now not beingCopy
, users have to explicitly useclone
, which makes potential allocations more obvious.This PR also changes
FontSystem::get_font_matches
to take animpl AsRef<Attrs> + Into<Attrs>
parameter, which it only clones if (a) the passed argument is a reference and (b) the cache does not contain an equivalentAttrs
. So this change leads to probably less allocations overall.