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

feat: rename Map::insert to Map::append and add proper insert impl #126

Merged
merged 1 commit into from
Dec 31, 2024
Merged
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
38 changes: 35 additions & 3 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@

/// 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` overwrites 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
Expand All @@ -59,6 +59,38 @@
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);

Check warning on line 66 in src/map.rs

View check run for this annotation

Codecov / codecov/patch

src/map.rs#L64-L66

Added lines #L64 - L66 were not covered by tests
}
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<K, V>(&mut self, key: K, value: V)
where
K: Into<Cow<'a, str>>,
V: Into<Cow<'a, str>>,
{
let key = key.into();
let value = value.into();

let i = self.inner.binary_search_by(|a| a.0.cmp(&key));
match i {
Ok(i) => {
Expand Down Expand Up @@ -191,7 +223,7 @@
}

{
map.insert("cache-control", "immutable");
map.append("cache-control", "immutable");
assert_eq!(map.len(), 2);
assert!(!map.is_empty());
assert!(map.get("nothing").is_none());
Expand Down
Loading