Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend map with referring insertion #60142

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
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
58 changes: 54 additions & 4 deletions src/liballoc/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,39 @@ impl<K: Ord, V> BTreeMap<K, V> {
}
}

/// Inserts a key-value pair into the map,
/// returning references to key and value.
pub fn insert_and_get_key_value(&mut self, key: K, value: V) -> (&K, &V) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function seems redundant; the references returned from _mut can be downgraded to immutable at the callsite.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same argument could be applied against get as there is get_mut.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite, as those take &self and &mut self, respectively, while the new methods always take &mut self

match self.entry(key) {
Occupied(mut entry) => {
entry.insert(value);
let (k, v) = entry.handle.into_kv_mut();
(&*k, &*v)
}
Vacant(entry) => {
let (k, v) = entry.insert_and_get_mut_key_value(value);
(&*k, &*v)
}
}
}

/// Inserts a key-value pair into the map,
/// returning an immutable reference to the key and
/// a mutable reference to value.
pub fn insert_and_get_mut_key_value(&mut self, key: K, value: V) -> (&K, &mut V) {
match self.entry(key) {
Occupied(mut entry) => {
entry.insert(value);
let (k, v) = entry.handle.into_kv_mut();
(&*k, v)
}
Vacant(entry) => {
let (k, v) = entry.insert_and_get_mut_key_value(value);
(&*k, v)
}
}
}

/// Removes a key from the map, returning the value at the key if the key
/// was previously in the map.
///
Expand Down Expand Up @@ -2272,6 +2305,16 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn insert(self, value: V) -> &'a mut V {
self.insert_and_get_mut_key_value(value).1
}

/// A modification of `insert`,
/// which extends its result with a mutable reference to key.
///
/// This function is intended for internal use and
/// is required for implementation of
/// `insert_and_get_key_value` and `insert_and_get_mut_key_value` on `BTreeMap`.
fn insert_and_get_mut_key_value(self, value: V) -> (&'a mut K, &'a mut V) {
*self.length += 1;

let out_ptr;
Expand All @@ -2281,7 +2324,7 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
let mut ins_edge;

let mut cur_parent = match self.handle.insert(self.key, value) {
(Fit(handle), _) => return handle.into_kv_mut().1,
(Fit(handle), _) => return handle.into_kv_mut(),
(Split(left, k, v, right), ptr) => {
ins_k = k;
ins_v = v;
Expand All @@ -2295,7 +2338,11 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
match cur_parent {
Ok(parent) => {
match parent.insert(ins_k, ins_v, ins_edge) {
Fit(_) => return unsafe { &mut *out_ptr },
Fit(handle) => {
let k = handle.into_kv_mut().0;
let v = unsafe { &mut *out_ptr };
return (k, v)
},
Split(left, k, v, right) => {
ins_k = k;
ins_v = v;
Expand All @@ -2305,8 +2352,11 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
}
}
Err(root) => {
root.push_level().push(ins_k, ins_v, ins_edge);
return unsafe { &mut *out_ptr };
let mut node = root.push_level();
node.push(ins_k, ins_v, ins_edge);
let k = node.first_kv().into_kv_mut().0;
let v = unsafe { &mut *out_ptr };
return (k, v)
}
}
}
Expand Down