diff --git a/src/map.rs b/src/map.rs index 756266e..2bfe219 100644 --- a/src/map.rs +++ b/src/map.rs @@ -39,14 +39,14 @@ impl<'a> Map<'a> { /// Insert a new element in this `Map` /// - /// If the `key` is already present, the `value` is appended to the existing value: + /// If the `key` is already present, the `value` overwrite the existing value: /// /// ``` /// let mut map = rusty_s3::Map::new(); /// map.insert("k", "a"); /// assert_eq!(map.get("k"), Some("a")); /// map.insert("k", "b"); - /// assert_eq!(map.get("k"), Some("a, b")); + /// assert_eq!(map.get("k"), Some("b")); /// ``` /// /// # Panics @@ -59,6 +59,38 @@ impl<'a> Map<'a> { let key = key.into(); let value = value.into(); + let i = self.inner.binary_search_by(|a| a.0.cmp(&key)); + match i { + Ok(i) => { + let old_value = self.inner.get_mut(i).expect("i can't be out of bounds"); + *old_value = (key, value); + } + Err(i) => self.inner.insert(i, (key, value)), + } + } + + /// Insert a new element in this `Map` + /// + /// If the `key` is already present, the `value` is appended to the existing value: + /// + /// ``` + /// let mut map = rusty_s3::Map::new(); + /// map.append("k", "a"); + /// assert_eq!(map.get("k"), Some("a")); + /// map.append("k", "b"); + /// assert_eq!(map.get("k"), Some("a, b")); + /// ``` + /// + /// # Panics + /// In case of out of bound inner index access + pub fn append(&mut self, key: K, value: V) + where + K: Into>, + V: Into>, + { + let key = key.into(); + let value = value.into(); + let i = self.inner.binary_search_by(|a| a.0.cmp(&key)); match i { Ok(i) => {