Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6c87448
optimize Cstr/EscapeAscii display
the8472 Jun 21, 2023
130b7e7
Add trait obligation tracking to FulfillCtxt and expose FnCtxt in rus…
gavinleroy Nov 21, 2023
50b4ca6
Teach tidy about line/col information for malformed features
oli-obk Jan 18, 2024
225f0b9
Make the remaining "private" fields actually private
oli-obk Jan 19, 2024
615946d
Stabilize simple offset_of
GKFX Dec 5, 2023
803b810
Remove feature(offset_of) from tests
GKFX Jan 5, 2024
7924c9b
Split remaining offset_of features into new tracking issues
GKFX Jan 19, 2024
0943a6b
add test issue-117965
trevyn Jan 19, 2024
4459be7
Added NonZeroXxx::from_mut(_unchecked)?
SOF3 Oct 29, 2022
3acb445
Added assert_unsafe_precondition! check for NonZeroXxx::from_mut_unch…
SOF3 Oct 30, 2022
596410e
Assign tracking issue number for feature(nonzero_from_mut)
SOF3 Dec 30, 2022
de2575f
Don't delete any lifetimes with bounds
trevyn Jan 19, 2024
b72af9f
Stabilize `round_ties_even`
Jules-Bertholet Jan 19, 2024
b1688b4
Avoid ICE: Check diagnostic is error before downgrading
estebank Jan 8, 2024
6b7e6ea
Account for traits using self-trait by name without `dyn`
estebank Jan 9, 2024
7edbc95
Update tests after rebase
estebank Jan 19, 2024
078a979
Don't use ReErased to detect type test promotion failed
compiler-errors Jan 20, 2024
f1713b0
Rollup merge of #103730 - SOF3:nonzero-from-mut, r=Mark-Simulacrum,dt…
matthiaskrgr Jan 20, 2024
17c95b6
Rollup merge of #113142 - the8472:opt-cstr-display, r=Mark-Simulacrum
matthiaskrgr Jan 20, 2024
6f67208
Rollup merge of #118799 - GKFX:stabilize-simple-offsetof, r=wesleywiser
matthiaskrgr Jan 20, 2024
2de5ca2
Rollup merge of #119613 - gavinleroy:expose-obligations, r=lcnr
matthiaskrgr Jan 20, 2024
177d513
Rollup merge of #119752 - estebank:ice-ice, r=fmease
matthiaskrgr Jan 20, 2024
836bc69
Rollup merge of #120132 - oli-obk:helpful_tidy, r=Mark-Simulacrum
matthiaskrgr Jan 20, 2024
409949b
Rollup merge of #120135 - oli-obk:smir_private, r=celinval
matthiaskrgr Jan 20, 2024
b7c2ba7
Rollup merge of #120148 - trevyn:issue-117965, r=cjgillot
matthiaskrgr Jan 20, 2024
862d3fe
Rollup merge of #120150 - Jules-Bertholet:stabilize-round-ties-even, …
matthiaskrgr Jan 20, 2024
bb816e6
Rollup merge of #120155 - compiler-errors:no-erased-when-promoting, r…
matthiaskrgr Jan 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added NonZeroXxx::from_mut(_unchecked)?
  • Loading branch information
SOF3 authored and dtolnay committed Jan 19, 2024
commit 4459be7bd59c781c850ab48679ef3a1e420e9f99
26 changes: 26 additions & 0 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,32 @@ macro_rules! nonzero_integer {
}
}

/// Converts a primitive mutable reference to a non-zero mutable reference
/// if the referenced integer is not zero.
#[unstable(feature = "nonzero_from_mut", issue = "none")]
#[must_use]
#[inline]
pub fn from_mut(n: &mut $Int) -> Option<&mut Self> {
// SAFETY: Self is repr(transparent), and the value is non-zero.
// As long as the returned reference is alive,
// the user cannot `*n = 0` directly.
(*n != 0).then(|| unsafe { &mut *(n as *mut $Int as *mut Self) })
}

/// Converts a primitive mutable reference to a non-zero mutable reference
/// without checking whether the referenced value is non-zero.
/// This results in undefined behavior if `*n` is zero.
///
/// # Safety
/// The referenced value must not be currently zero.
#[unstable(feature = "nonzero_from_mut", issue = "none")]
#[must_use]
#[inline]
pub unsafe fn from_mut_unchecked(n: &mut $Int) -> &mut Self {
// SAFETY: Self is repr(transparent), and the value is assumed to be non-zero.
unsafe { &mut *(n as *mut $Int as *mut Self) }
}

/// Returns the value as a primitive type.
#[$stability]
#[inline]
Expand Down