Skip to content

Commit e583132

Browse files
authored
Rollup merge of #82977 - camsteffen:opt-get-insert-def, r=m-ou-se
Rename `Option::get_or_default` to `get_or_insert_default` ...as [suggested](#82901 (comment)) by `@m-ou-se.` In hindsight this seems rather obvious, at least to me. r? `@joshtriplett`
2 parents f5196ae + b0514a6 commit e583132

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

compiler/rustc_mir/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Rust MIR: a lowered representation of Rust.
2525
#![feature(stmt_expr_attributes)]
2626
#![feature(trait_alias)]
2727
#![feature(option_expect_none)]
28-
#![feature(option_get_or_default)]
28+
#![feature(option_get_or_insert_default)]
2929
#![feature(or_patterns)]
3030
#![feature(once_cell)]
3131
#![feature(control_flow_enum)]

compiler/rustc_mir/src/transform/coverage/graph.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ impl BasicCoverageBlockData {
392392
}
393393
}
394394
let operand = counter_kind.as_operand_id();
395-
if let Some(replaced) = self.edge_from_bcbs.get_or_default().insert(from_bcb, counter_kind)
395+
if let Some(replaced) =
396+
self.edge_from_bcbs.get_or_insert_default().insert(from_bcb, counter_kind)
396397
{
397398
Error::from_string(format!(
398399
"attempt to set an edge counter more than once; from_bcb: \

library/core/src/option.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -854,55 +854,55 @@ impl<T> Option<T> {
854854
// Entry-like operations to insert if None and return a reference
855855
/////////////////////////////////////////////////////////////////////////
856856

857-
/// Inserts the default value into the option if it is [`None`], then
857+
/// Inserts `value` into the option if it is [`None`], then
858858
/// returns a mutable reference to the contained value.
859859
///
860860
/// # Examples
861861
///
862862
/// ```
863-
/// #![feature(option_get_or_default)]
864-
///
865863
/// let mut x = None;
866864
///
867865
/// {
868-
/// let y: &mut u32 = x.get_or_default();
869-
/// assert_eq!(y, &0);
866+
/// let y: &mut u32 = x.get_or_insert(5);
867+
/// assert_eq!(y, &5);
870868
///
871869
/// *y = 7;
872870
/// }
873871
///
874872
/// assert_eq!(x, Some(7));
875873
/// ```
876874
#[inline]
877-
#[unstable(feature = "option_get_or_default", issue = "82901")]
878-
pub fn get_or_default(&mut self) -> &mut T
879-
where
880-
T: Default,
881-
{
882-
self.get_or_insert_with(Default::default)
875+
#[stable(feature = "option_entry", since = "1.20.0")]
876+
pub fn get_or_insert(&mut self, value: T) -> &mut T {
877+
self.get_or_insert_with(|| value)
883878
}
884879

885-
/// Inserts `value` into the option if it is [`None`], then
880+
/// Inserts the default value into the option if it is [`None`], then
886881
/// returns a mutable reference to the contained value.
887882
///
888883
/// # Examples
889884
///
890885
/// ```
886+
/// #![feature(option_get_or_insert_default)]
887+
///
891888
/// let mut x = None;
892889
///
893890
/// {
894-
/// let y: &mut u32 = x.get_or_insert(5);
895-
/// assert_eq!(y, &5);
891+
/// let y: &mut u32 = x.get_or_insert_default();
892+
/// assert_eq!(y, &0);
896893
///
897894
/// *y = 7;
898895
/// }
899896
///
900897
/// assert_eq!(x, Some(7));
901898
/// ```
902899
#[inline]
903-
#[stable(feature = "option_entry", since = "1.20.0")]
904-
pub fn get_or_insert(&mut self, value: T) -> &mut T {
905-
self.get_or_insert_with(|| value)
900+
#[unstable(feature = "option_get_or_insert_default", issue = "82901")]
901+
pub fn get_or_insert_default(&mut self) -> &mut T
902+
where
903+
T: Default,
904+
{
905+
self.get_or_insert_with(Default::default)
906906
}
907907

908908
/// Inserts a value computed from `f` into the option if it is [`None`],

0 commit comments

Comments
 (0)