From e8ba82d5c0f5f0d57de9f006d2b80869b6b9ec19 Mon Sep 17 00:00:00 2001 From: Louis Maddox Date: Sun, 12 Oct 2025 17:10:22 +0100 Subject: [PATCH] feat: indexmap support --- Cargo.toml | 4 +++ src/object.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 5a97519..6f29056 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ rust-version = "1.85" itoa = "1" ryu = "1" halfbrown = { version = "0.4", optional = true } +indexmap = { version = "2.0", optional = true } float-cmp = "0.10" ordered-float = { version = "5", optional = true } hashbrown = { version = "0.16", optional = true } @@ -31,6 +32,9 @@ custom-types = [] # Support for abi-stable's `StableAbi` implementation - INCOMPATIBLE WITH ordered-float c-abi = ["abi_stable"] +# Support for ordered maps +indexmap = ["dep:indexmap"] + # use runtime detection of the CPU features where possible instead of enforcing an instruction set runtime-detection = [] diff --git a/src/object.rs b/src/object.rs index bb0a3a3..61deb5a 100644 --- a/src/object.rs +++ b/src/object.rs @@ -2,6 +2,8 @@ use halfbrown::HashMap as Halfbrown; #[cfg(feature = "hashbrown")] use hashbrown::HashMap as Hashbrown; +#[cfg(feature = "indexmap")] +use indexmap::IndexMap; use std::collections::HashMap; use std::hash::Hash; use std::{borrow::Borrow, hash::BuildHasher}; @@ -151,6 +153,81 @@ where } } +#[cfg(feature = "indexmap")] +impl Object for IndexMap +where + MapK: Hash + Eq, +{ + type Key = MapK; + type Element = MapE; + + #[inline] + fn get(&self, k: &Q) -> Option<&Self::Element> + where + Self::Key: Borrow, + Q: ?Sized + Hash + Eq + Ord, + { + IndexMap::get(self, k) + } + + #[inline] + fn iter(&self) -> Box + '_> { + Box::new(IndexMap::iter(self)) + } + + #[inline] + fn keys(&self) -> Box + '_> { + Box::new(IndexMap::keys(self)) + } + + #[inline] + fn values(&self) -> Box + '_> { + Box::new(IndexMap::values(self)) + } + + #[inline] + fn len(&self) -> usize { + IndexMap::len(self) + } +} + +#[cfg(feature = "indexmap")] +impl ObjectMut for IndexMap +where + MapK: Hash + Eq, +{ + type Key = MapK; + type Element = MapE; + + #[inline] + fn get_mut(&mut self, k: &Q) -> Option<&mut Self::Element> + where + Self::Key: Borrow, + Q: ?Sized + Hash + Eq + Ord, + { + IndexMap::get_mut(self, k) + } + + #[inline] + fn insert(&mut self, k: K, v: V) -> Option + where + K: Into, + V: Into, + Self::Key: Hash + Eq, + { + IndexMap::insert(self, k.into(), v.into()) + } + + #[inline] + fn remove(&mut self, k: &Q) -> Option + where + Self::Key: Borrow, + Q: ?Sized + Hash + Eq + Ord, + { + IndexMap::shift_remove(self, k) + } +} + impl Object for HashMap where MapK: Hash + Eq,