Rust update.

This commit is contained in:
Chris Morgan 2015-03-25 17:59:11 +11:00
parent 143ee06268
commit 97ec79029f
2 changed files with 42 additions and 18 deletions

View file

@ -1,10 +1,8 @@
//! This crate provides the `AnyMap` type, a safe and convenient store for one value of each type. //! This crate provides the `AnyMap` type, a safe and convenient store for one value of each type.
#![feature(core, std_misc, hash)] #![feature(core, std_misc)]
#![cfg_attr(test, feature(test))] #![cfg_attr(test, feature(test))]
#![warn(unused_qualifications, non_upper_case_globals, #![warn(missing_docs, unused_results)]
variant_size_differences, unused_typecasts,
missing_docs, unused_results)]
#[cfg(test)] #[cfg(test)]
extern crate test; extern crate test;
@ -235,11 +233,21 @@ pub enum Entry<'a, V: 'a> {
} }
impl<'a, V: Any + Clone> Entry<'a, V> { impl<'a, V: Any + Clone> Entry<'a, V> {
/// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant /// Ensures a value is in the entry by inserting the default if empty, and returns
pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, V>> { /// a mutable reference to the value in the entry.
pub fn or_insert(self, default: V) -> &'a mut V {
match self { match self {
Entry::Occupied(inner) => Ok(inner.into_mut()), Entry::Occupied(inner) => inner.into_mut(),
Entry::Vacant(inner) => Err(inner), Entry::Vacant(inner) => inner.insert(default),
}
}
/// Ensures a value is in the entry by inserting the result of the default function if empty,
/// and returns a mutable reference to the value in the entry.
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
match self {
Entry::Occupied(inner) => inner.into_mut(),
Entry::Vacant(inner) => inner.insert(default()),
} }
} }
} }

View file

@ -224,17 +224,17 @@ impl RawAnyMap {
} }
impl<Q: ?Sized> Index<Q> for RawAnyMap where TypeId: Borrow<Q>, Q: Eq + Hash { impl<Q> Index<Q> for RawAnyMap where TypeId: Borrow<Q>, Q: Eq + Hash {
type Output = Any; type Output = Any;
fn index<'a>(&'a self, index: &Q) -> &'a Any { fn index<'a>(&'a self, index: Q) -> &'a Any {
self.get(index).expect("no entry found for key") self.get(&index).expect("no entry found for key")
} }
} }
impl<Q: ?Sized> IndexMut<Q> for RawAnyMap where TypeId: Borrow<Q>, Q: Eq + Hash { impl<Q> IndexMut<Q> for RawAnyMap where TypeId: Borrow<Q>, Q: Eq + Hash {
fn index_mut<'a>(&'a mut self, index: &Q) -> &'a mut Any { fn index_mut<'a>(&'a mut self, index: Q) -> &'a mut Any {
self.get_mut(index).expect("no entry found for key") self.get_mut(&index).expect("no entry found for key")
} }
} }
@ -266,11 +266,27 @@ pub enum Entry<'a> {
} }
impl<'a> Entry<'a> { impl<'a> Entry<'a> {
/// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant. /// Ensures a value is in the entry by inserting the default if empty, and returns
pub fn get(self) -> Result<&'a mut Any, VacantEntry<'a>> { /// a mutable reference to the value in the entry.
///
/// It is the callers responsibility to ensure that the key of the entry corresponds with
/// the type ID of `value`. If they do not, memory safety may be violated.
pub unsafe fn or_insert(self, default: Box<Any>) -> &'a mut Any {
match self { match self {
Entry::Occupied(inner) => Ok(inner.into_mut()), Entry::Occupied(inner) => inner.into_mut(),
Entry::Vacant(inner) => Err(inner), Entry::Vacant(inner) => inner.insert(default),
}
}
/// Ensures a value is in the entry by inserting the result of the default function if empty,
/// and returns a mutable reference to the value in the entry.
///
/// It is the callers responsibility to ensure that the key of the entry corresponds with
/// the type ID of `value`. If they do not, memory safety may be violated.
pub unsafe fn or_insert_with<F: FnOnce() -> Box<Any>>(self, default: F) -> &'a mut Any {
match self {
Entry::Occupied(inner) => inner.into_mut(),
Entry::Vacant(inner) => inner.insert(default()),
} }
} }
} }