mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
Option::unwrap
is now const (#20007)
Summary -- I noticed while working on #20006 that we had a custom `unwrap` function for `Option`. This has been const on stable since 1.83 ([docs](https://doc.rust-lang.org/std/option/enum.Option.html#method.unwrap), [release notes](https://blog.rust-lang.org/2024/11/28/Rust-1.83.0/)), so I think it's safe to use now. I grepped a bit for related todos and found this one for `AsciiCharSet` but no others. Test Plan -- Existing tests
This commit is contained in:
parent
ddd4bab67c
commit
1a38831d53
2 changed files with 4 additions and 18 deletions
|
@ -93,12 +93,7 @@ impl NamedCharset {
|
||||||
name,
|
name,
|
||||||
bytes,
|
bytes,
|
||||||
// SAFETY: The named charset is guaranteed to have only ascii bytes.
|
// SAFETY: The named charset is guaranteed to have only ascii bytes.
|
||||||
// TODO: replace with `.unwrap()`, when `Option::unwrap` will be stable in `const fn`
|
ascii_char_set: AsciiCharSet::from_bytes(bytes).unwrap(),
|
||||||
// https://github.com/rust-lang/rust/issues/67441
|
|
||||||
ascii_char_set: match AsciiCharSet::from_bytes(bytes) {
|
|
||||||
Some(ascii_char_set) => ascii_char_set,
|
|
||||||
None => unreachable!(),
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -562,11 +562,11 @@ pub struct OneIndexed(NonZeroUsize);
|
||||||
|
|
||||||
impl OneIndexed {
|
impl OneIndexed {
|
||||||
/// The largest value that can be represented by this integer type
|
/// The largest value that can be represented by this integer type
|
||||||
pub const MAX: Self = unwrap(Self::new(usize::MAX));
|
pub const MAX: Self = Self::new(usize::MAX).unwrap();
|
||||||
// SAFETY: These constants are being initialized with non-zero values
|
// SAFETY: These constants are being initialized with non-zero values
|
||||||
/// The smallest value that can be represented by this integer type.
|
/// The smallest value that can be represented by this integer type.
|
||||||
pub const MIN: Self = unwrap(Self::new(1));
|
pub const MIN: Self = Self::new(1).unwrap();
|
||||||
pub const ONE: NonZeroUsize = unwrap(NonZeroUsize::new(1));
|
pub const ONE: NonZeroUsize = NonZeroUsize::new(1).unwrap();
|
||||||
|
|
||||||
/// Creates a non-zero if the given value is not zero.
|
/// Creates a non-zero if the given value is not zero.
|
||||||
pub const fn new(value: usize) -> Option<Self> {
|
pub const fn new(value: usize) -> Option<Self> {
|
||||||
|
@ -636,15 +636,6 @@ impl fmt::Display for OneIndexed {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A const `Option::unwrap` without nightly features:
|
|
||||||
/// [Tracking issue](https://github.com/rust-lang/rust/issues/67441)
|
|
||||||
const fn unwrap<T: Copy>(option: Option<T>) -> T {
|
|
||||||
match option {
|
|
||||||
Some(value) => value,
|
|
||||||
None => panic!("unwrapping None"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for OneIndexed {
|
impl FromStr for OneIndexed {
|
||||||
type Err = ParseIntError;
|
type Err = ParseIntError;
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue