Unify Attrs and AttrsOwned
#100
Open
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
Attrsand the owningAttrsOwned, which only differ in their usage ofFamilyvsFamilyOwned. In theory, this allows passingAttrswithout having to allocate. In practice, we have the following situation:FontSystem::get_font_matcheswants 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.AttrsOwnedinAttrsList, so it is almost impossible to not have anAttrsturn into anAttrsOwnedsomewhere down the line.This PR removes the old
Attrs, renamesAttrsOwnedtoAttrsand adds anAttrsBuilder, which has the same helper methods as the oldAttrs. This PR also includes several changes to prevent allocations in many cases:FamilyOwneduses aCow<'static, str>instead ofString, allowing the use of string literals without allocating.AttrsBuilder::familytakes animpl Into<FamilyOwned>, which is only implemented forFamilyOwnedorFamily<'static>, so users need to explicity useFamilyOwned::newif they want to use a non staticFamily.AttrsimplementsAsRef<Attrs>andFrom<&Attrs>, allowing methods that might need to store theAttrsto accept both values and references ofAttrs, while at the same time preventing an extra allocation when a value is passed.Attrsnow not beingCopy, users have to explicitly useclone, which makes potential allocations more obvious.This PR also changes
FontSystem::get_font_matchesto 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.