mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
Add an opaque `Set` newtype paralleling `Object`, living under `src/value/set/` with the same module structure (`mod.rs` / `iter.rs` / `serde.rs`). `Set` wraps `BTreeSet<Value>` today but exposes only a curated surface: `contains`, `insert`, `remove`, `iter`, `iter_sorted`, `cursor` (resumable), `is_subset`, `intersection`, `difference`, serde, and a hand-written `Ord`. The cursor types are re-exported behind the `rvm` feature so the follow-up `IterationState::Set` swap can land additively. To free the `Set` name for the new public type, the crate-internal `BTreeSet as Set` / `HashSet as Set` aliases in `lib.rs` are renamed to `MapSet`. All in-tree consumers of the old alias are updated in lockstep. `Value::Set` is unchanged in this commit (still wraps `Rc<BTreeSet<Value>>`); the payload swap and call-site migration ship in the next PR. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
104 lines
2.2 KiB
Rust
104 lines
2.2 KiB
Rust
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
//! Opaque iterator types for [`Set`].
|
|
//!
|
|
//! These newtypes wrap the storage backend's iterators so the backend can be
|
|
//! swapped without changing any iterator type signatures observed by callers.
|
|
|
|
use alloc::collections::btree_set;
|
|
use core::iter::FusedIterator;
|
|
|
|
use super::Set;
|
|
use crate::value::Value;
|
|
|
|
/// Owned iterator over `Value` elements.
|
|
#[derive(Debug)]
|
|
pub struct IntoIter {
|
|
pub(super) inner: btree_set::IntoIter<Value>,
|
|
}
|
|
|
|
impl Iterator for IntoIter {
|
|
type Item = Value;
|
|
#[inline]
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
self.inner.next()
|
|
}
|
|
#[inline]
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
self.inner.size_hint()
|
|
}
|
|
}
|
|
|
|
impl DoubleEndedIterator for IntoIter {
|
|
#[inline]
|
|
fn next_back(&mut self) -> Option<Self::Item> {
|
|
self.inner.next_back()
|
|
}
|
|
}
|
|
|
|
impl ExactSizeIterator for IntoIter {
|
|
#[inline]
|
|
fn len(&self) -> usize {
|
|
self.inner.len()
|
|
}
|
|
}
|
|
|
|
impl FusedIterator for IntoIter {}
|
|
|
|
/// Borrowed iterator over `&Value` elements.
|
|
#[derive(Debug, Clone)]
|
|
pub struct Iter<'a> {
|
|
pub(super) inner: btree_set::Iter<'a, Value>,
|
|
}
|
|
|
|
impl<'a> Iterator for Iter<'a> {
|
|
type Item = &'a Value;
|
|
#[inline]
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
self.inner.next()
|
|
}
|
|
#[inline]
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
self.inner.size_hint()
|
|
}
|
|
}
|
|
|
|
impl<'a> DoubleEndedIterator for Iter<'a> {
|
|
#[inline]
|
|
fn next_back(&mut self) -> Option<Self::Item> {
|
|
self.inner.next_back()
|
|
}
|
|
}
|
|
|
|
impl<'a> ExactSizeIterator for Iter<'a> {
|
|
#[inline]
|
|
fn len(&self) -> usize {
|
|
self.inner.len()
|
|
}
|
|
}
|
|
|
|
impl<'a> FusedIterator for Iter<'a> {}
|
|
|
|
impl IntoIterator for Set {
|
|
type Item = Value;
|
|
type IntoIter = IntoIter;
|
|
#[inline]
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
IntoIter {
|
|
inner: self.inner.into_iter(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a> IntoIterator for &'a Set {
|
|
type Item = &'a Value;
|
|
type IntoIter = Iter<'a>;
|
|
#[inline]
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
Iter {
|
|
inner: self.inner.iter(),
|
|
}
|
|
}
|
|
}
|