Should CodepointOrGlyphId be more static?
Created by: jimblandy
I noticed that when CodePointOrGlyphId
appears in a public function's
signature, it's always as Into<CodePointOrGlyphId>
. If the purpose here is to
make functions like Font::glyph
accept either a Codepoint
, GlyphId
, or
char
, then it seems nicer to use the types to select the necessary conversions
at compile time, rather than constructing an enum, immediately matching on it,
and counting on the compiler to optimize that all out.
Instead of CodePointOrGlyphId
, rusttype
could offer a trait:
trait IntoGlyphId {
fn into_glyph_id(self, &Font) -> GlyphId;
}
with implementations for char
, GlyphId
, and Codepoint
:
impl IntoGlyphId for char {
fn into_glyph_id(self, font: &Font) -> GlyphId {
GlyphId(font.info.find_glyph_index(self))
}
}
impl IntoGlyphId for GlyphId {
fn into_glyph_id(self, font: &Font) -> GlyphId {
GlyphId(self)
}
}
I implemented this, and tried it out on the four most-downloaded crates that
depend on rusttype
. None of the crates use CodePointOrGlyphId
directly, and
all compiled without source changes.
What do you think?