Skip to content

Commit 1c35953

Browse files
committed
entry API v3: replace Entry::get with Entry::default and Entry::default_with
1 parent 199bdcf commit 1c35953

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

src/libcollections/btree/map.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,15 +1143,39 @@ impl<'a, K, V> DoubleEndedIterator for RangeMut<'a, K, V> {
11431143
}
11441144

11451145
impl<'a, K: Ord, V> Entry<'a, K, V> {
1146-
/// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
11471146
#[unstable(feature = "std_misc",
11481147
reason = "will soon be replaced by or_insert")]
1148+
#[deprecated(since = "1.0",
1149+
reason = "replaced with more ergonomic `default` and `default_with`")]
1150+
/// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
11491151
pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, K, V>> {
11501152
match self {
11511153
Occupied(entry) => Ok(entry.into_mut()),
11521154
Vacant(entry) => Err(entry),
11531155
}
11541156
}
1157+
1158+
#[unstable(feature = "collections",
1159+
reason = "matches entry v3 specification, waiting for dust to settle")]
1160+
/// Ensures a value is in the entry by inserting the default if empty, and returns
1161+
/// a mutable reference to the value in the entry.
1162+
pub fn default(self, default: V) -> &'a mut V {
1163+
match self {
1164+
Occupied(entry) => entry.into_mut(),
1165+
Vacant(entry) => entry.insert(default),
1166+
}
1167+
}
1168+
1169+
#[unstable(feature = "collections",
1170+
reason = "matches entry v3 specification, waiting for dust to settle")]
1171+
/// Ensures a value is in the entry by inserting the result of the default function if empty,
1172+
/// and returns a mutable reference to the value in the entry.
1173+
pub fn default_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
1174+
match self {
1175+
Occupied(entry) => entry.into_mut(),
1176+
Vacant(entry) => entry.insert(default()),
1177+
}
1178+
}
11551179
}
11561180

11571181
impl<'a, K: Ord, V> VacantEntry<'a, K, V> {

src/libcollections/vec_map.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,13 +675,37 @@ impl<V> VecMap<V> {
675675
impl<'a, V> Entry<'a, V> {
676676
#[unstable(feature = "collections",
677677
reason = "will soon be replaced by or_insert")]
678+
#[deprecated(since = "1.0",
679+
reason = "replaced with more ergonomic `default` and `default_with`")]
678680
/// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
679681
pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, V>> {
680682
match self {
681683
Occupied(entry) => Ok(entry.into_mut()),
682684
Vacant(entry) => Err(entry),
683685
}
684686
}
687+
688+
#[unstable(feature = "collections",
689+
reason = "matches entry v3 specification, waiting for dust to settle")]
690+
/// Ensures a value is in the entry by inserting the default if empty, and returns
691+
/// a mutable reference to the value in the entry.
692+
pub fn default(self, default: V) -> &'a mut V {
693+
match self {
694+
Occupied(entry) => entry.into_mut(),
695+
Vacant(entry) => entry.insert(default),
696+
}
697+
}
698+
699+
#[unstable(feature = "collections",
700+
reason = "matches entry v3 specification, waiting for dust to settle")]
701+
/// Ensures a value is in the entry by inserting the result of the default function if empty,
702+
/// and returns a mutable reference to the value in the entry.
703+
pub fn default_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
704+
match self {
705+
Occupied(entry) => entry.into_mut(),
706+
Vacant(entry) => entry.insert(default()),
707+
}
708+
}
685709
}
686710

687711
impl<'a, V> VacantEntry<'a, V> {

src/libstd/collections/hash/map.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use hash::{Hash, SipHasher};
2323
use iter::{self, Iterator, ExactSizeIterator, IntoIterator, IteratorExt, FromIterator, Extend, Map};
2424
use marker::Sized;
2525
use mem::{self, replace};
26-
use ops::{Deref, FnMut, Index};
26+
use ops::{Deref, FnMut, FnOnce, Index};
2727
use option::Option::{self, Some, None};
2828
use rand::{self, Rng};
2929
use result::Result::{self, Ok, Err};
@@ -1488,12 +1488,36 @@ impl<'a, K, V> Entry<'a, K, V> {
14881488
/// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant.
14891489
#[unstable(feature = "std_misc",
14901490
reason = "will soon be replaced by or_insert")]
1491+
#[deprecated(since = "1.0",
1492+
reason = "replaced with more ergonomic `default` and `default_with`")]
14911493
pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, K, V>> {
14921494
match self {
14931495
Occupied(entry) => Ok(entry.into_mut()),
14941496
Vacant(entry) => Err(entry),
14951497
}
14961498
}
1499+
1500+
#[unstable(feature = "collections",
1501+
reason = "matches entry v3 specification, waiting for dust to settle")]
1502+
/// Ensures a value is in the entry by inserting the default if empty, and returns
1503+
/// a mutable reference to the value in the entry.
1504+
pub fn default(self, default: V) -> &'a mut V {
1505+
match self {
1506+
Occupied(entry) => entry.into_mut(),
1507+
Vacant(entry) => entry.insert(default),
1508+
}
1509+
}
1510+
1511+
#[unstable(feature = "collections",
1512+
reason = "matches entry v3 specification, waiting for dust to settle")]
1513+
/// Ensures a value is in the entry by inserting the result of the default function if empty,
1514+
/// and returns a mutable reference to the value in the entry.
1515+
pub fn default_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
1516+
match self {
1517+
Occupied(entry) => entry.into_mut(),
1518+
Vacant(entry) => entry.insert(default()),
1519+
}
1520+
}
14971521
}
14981522

14991523
impl<'a, K, V> OccupiedEntry<'a, K, V> {

0 commit comments

Comments
 (0)