Skip to content

Commit e8f45e7

Browse files
committed
Record{,Owned}::key_value return KeyValues instead of iterator
1 parent 9cc8484 commit e8f45e7

File tree

3 files changed

+62
-43
lines changed

3 files changed

+62
-43
lines changed

spdlog/src/kv.rs

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{borrow::Cow, fmt, marker::PhantomData};
1+
use std::{borrow::Cow, fmt, slice};
22

33
use value_bag::{OwnedValueBag, ValueBag};
44

@@ -73,40 +73,50 @@ impl KeyOwned {
7373
pub type Value<'a> = ValueBag<'a>;
7474
pub(crate) type ValueOwned = OwnedValueBag;
7575

76-
pub struct KeyValuesIter<'a, I> {
77-
iter: I,
78-
len: usize,
79-
phantom: PhantomData<&'a ()>,
76+
enum KeyValuesInner<'a> {
77+
Borrowed(&'a [Pair<'a>]),
78+
Owned(&'a [(KeyOwned, ValueOwned)]),
8079
}
80+
enum KeyValuesIterInner<'a> {
81+
Borrowed(slice::Iter<'a, Pair<'a>>),
82+
Owned(slice::Iter<'a, (KeyOwned, ValueOwned)>),
83+
}
84+
85+
pub struct KeyValues<'a>(KeyValuesInner<'a>);
8186

82-
impl<I> KeyValuesIter<'_, I> {
87+
impl<'a> KeyValues<'a> {
8388
pub fn len(&self) -> usize {
84-
self.len
89+
match self.0 {
90+
KeyValuesInner::Borrowed(p) => p.len(),
91+
KeyValuesInner::Owned(p) => p.len(),
92+
}
8593
}
8694

8795
pub fn is_empty(&self) -> bool {
88-
self.len == 0
96+
match self.0 {
97+
KeyValuesInner::Borrowed(p) => p.is_empty(),
98+
KeyValuesInner::Owned(p) => p.is_empty(),
99+
}
89100
}
90-
}
91101

92-
impl<'a, I> KeyValuesIter<'a, I>
93-
where
94-
I: Iterator<Item = (Key<'a>, Value<'a>)>,
95-
{
96-
pub(crate) fn new(iter: I, len: usize) -> Self {
97-
Self {
98-
iter,
99-
len,
100-
phantom: PhantomData,
102+
pub fn iter(&self) -> KeyValuesIter<'a> {
103+
match &self.0 {
104+
KeyValuesInner::Borrowed(p) => KeyValuesIter(KeyValuesIterInner::Borrowed(p.iter())),
105+
KeyValuesInner::Owned(p) => KeyValuesIter(KeyValuesIterInner::Owned(p.iter())),
101106
}
102107
}
103108

104-
pub(crate) fn write_to(
105-
mut self,
106-
dest: &mut impl fmt::Write,
107-
leading_space: bool,
108-
) -> fmt::Result {
109-
let first = self.next();
109+
pub(crate) fn with_borrowed(pairs: &'a [Pair<'a>]) -> Self {
110+
Self(KeyValuesInner::Borrowed(pairs))
111+
}
112+
113+
pub(crate) fn with_owned(pairs: &'a [(KeyOwned, ValueOwned)]) -> Self {
114+
Self(KeyValuesInner::Owned(pairs))
115+
}
116+
117+
pub(crate) fn write_to(&self, dest: &mut impl fmt::Write, leading_space: bool) -> fmt::Result {
118+
let mut iter = self.iter();
119+
let first = iter.next();
110120
if let Some((key, value)) = first {
111121
if leading_space {
112122
dest.write_str(" { ")?;
@@ -120,7 +130,7 @@ where
120130
dest.write_str("=")?;
121131
write!(dest, "{}", value)?;
122132

123-
for (key, value) in self {
133+
for (key, value) in iter {
124134
dest.write_str(", ")?;
125135
dest.write_str(key.as_str())?;
126136
dest.write_str("=")?;
@@ -133,18 +143,33 @@ where
133143
}
134144
}
135145

136-
impl<'a, I> Iterator for KeyValuesIter<'a, I>
137-
where
138-
I: Iterator<Item = (Key<'a>, Value<'a>)>,
139-
{
140-
type Item = I::Item;
146+
impl<'a> IntoIterator for KeyValues<'a> {
147+
type Item = Pair<'a>;
148+
type IntoIter = KeyValuesIter<'a>;
149+
150+
fn into_iter(self) -> Self::IntoIter {
151+
self.iter()
152+
}
153+
}
154+
155+
pub struct KeyValuesIter<'a>(KeyValuesIterInner<'a>);
156+
157+
impl<'a> Iterator for KeyValuesIter<'a> {
158+
type Item = Pair<'a>;
141159

142160
fn next(&mut self) -> Option<Self::Item> {
143-
self.iter.next()
161+
match &mut self.0 {
162+
// The 2 clones should be cheap
163+
KeyValuesIterInner::Borrowed(iter) => iter.next().map(|(k, v)| (k.clone(), v.clone())),
164+
KeyValuesIterInner::Owned(iter) => iter.next().map(|(k, v)| (k.as_ref(), v.by_ref())),
165+
}
144166
}
145167

146168
fn size_hint(&self) -> (usize, Option<usize>) {
147-
self.iter.size_hint()
169+
match &self.0 {
170+
KeyValuesIterInner::Borrowed(iter) => iter.size_hint(),
171+
KeyValuesIterInner::Owned(iter) => iter.size_hint(),
172+
}
148173
}
149174
}
150175

spdlog/src/log_macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ mod tests {
379379
(
380380
record
381381
.key_values()
382+
.into_iter()
382383
.map(|(k, v)| (k.inner(), v.to_string()))
383384
.collect::<Vec<_>>(),
384385
record.level(),

spdlog/src/record.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,8 @@ impl<'a> Record<'a> {
110110
}
111111

112112
#[must_use]
113-
pub fn key_values(&self) -> kv::KeyValuesIter<impl Iterator<Item = (kv::Key, kv::Value)>> {
114-
kv::KeyValuesIter::new(
115-
// The 2 clones should be cheap
116-
self.kvs.iter().map(|(k, v)| (k.clone(), v.clone())),
117-
self.kvs.len(),
118-
)
113+
pub fn key_values(&self) -> kv::KeyValues {
114+
kv::KeyValues::with_borrowed(&self.kvs)
119115
}
120116

121117
// When adding more getters, also add to `RecordOwned`
@@ -242,11 +238,8 @@ impl RecordOwned {
242238
}
243239

244240
#[must_use]
245-
pub fn key_values(&self) -> kv::KeyValuesIter<impl Iterator<Item = (kv::Key, kv::Value)>> {
246-
kv::KeyValuesIter::new(
247-
self.kvs.iter().map(|(k, v)| (k.as_ref(), v.by_ref())),
248-
self.kvs.len(),
249-
)
241+
pub fn key_values(&self) -> kv::KeyValues {
242+
kv::KeyValues::with_owned(&self.kvs)
250243
}
251244

252245
// When adding more getters, also add to `Record`

0 commit comments

Comments
 (0)