Skip to content

Commit

Permalink
Use TryFrom instead of associated function
Browse files Browse the repository at this point in the history
  • Loading branch information
fee1-dead committed Nov 12, 2021
1 parent 9b3bd7c commit 15a11ea
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,17 @@ impl GlyphId {
}

pub fn as_char(self) -> Option<char> {
use std::convert::TryFrom;
let value = self.value();

if value & C_BIT == 0 {
None
} else {
// SAFETY: this is safe because we never construct a `GlyphId` with the C_BIT set
// with an invalid character.
unsafe { Some(char::from_u32_unchecked(value & C_MASK)) }
match char::try_from(value & C_MASK) {
Ok(c) => Some(c),
// we never construct a `GlyphId` with the C_BIT set with an invalid character.
Err(_) => unreachable!(),
}
}
}
}
Expand Down

0 comments on commit 15a11ea

Please sign in to comment.