Skip to content

Commit

Permalink
Improve the safety documentation on new_unchecked
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmcm committed Jan 11, 2025
1 parent 6f2a783 commit ebd6d3f
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion library/core/src/num/niche_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ macro_rules! define_valid_range_type {
};

impl $name {
/// Constructs an instance of this type from the underlying integer
/// primitive without checking whether its zero.
///
/// # Safety
/// Immediate language UB if `val == 0`, as it violates the validity
/// invariant of this type.
#[inline]
pub const unsafe fn new_unchecked(val: $int) -> Self {
// SAFETY: same precondition
// SAFETY: Caller promised that `val` is non-zero.
unsafe { $name(val) }
}

Expand Down

0 comments on commit ebd6d3f

Please sign in to comment.