Skip to content

Commit 6743f4a

Browse files
committed
make value internal visitor support borrowing
1 parent ddb118e commit 6743f4a

File tree

4 files changed

+216
-96
lines changed

4 files changed

+216
-96
lines changed

src/kv/value/internal.rs

Lines changed: 91 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,9 @@ pub(super) enum Inner<'v> {
2424
}
2525

2626
impl<'v> Inner<'v> {
27-
pub(super) fn visit(&self, visitor: &mut dyn Visitor) -> Result<(), Error> {
28-
match *self {
29-
Inner::Primitive(value) => match value {
30-
Primitive::Signed(value) => visitor.i64(value),
31-
Primitive::Unsigned(value) => visitor.u64(value),
32-
Primitive::Float(value) => visitor.f64(value),
33-
Primitive::Bool(value) => visitor.bool(value),
34-
Primitive::Char(value) => visitor.char(value),
35-
Primitive::Str(value) => visitor.str(value),
36-
Primitive::None => visitor.none(),
37-
},
27+
pub(super) fn visit(self, visitor: &mut dyn Visitor<'v>) -> Result<(), Error> {
28+
match self {
29+
Inner::Primitive(value) => value.visit(visitor),
3830
Inner::Fill(value) => value.fill(&mut Slot::new(visitor)),
3931
Inner::Debug(value) => visitor.debug(value),
4032
Inner::Display(value) => visitor.display(value),
@@ -46,7 +38,7 @@ impl<'v> Inner<'v> {
4638
}
4739

4840
/// The internal serialization contract.
49-
pub(super) trait Visitor {
41+
pub(super) trait Visitor<'v> {
5042
fn debug(&mut self, v: &dyn fmt::Debug) -> Result<(), Error>;
5143
fn display(&mut self, v: &dyn fmt::Display) -> Result<(), Error> {
5244
self.debug(&format_args!("{}", v))
@@ -57,7 +49,12 @@ pub(super) trait Visitor {
5749
fn f64(&mut self, v: f64) -> Result<(), Error>;
5850
fn bool(&mut self, v: bool) -> Result<(), Error>;
5951
fn char(&mut self, v: char) -> Result<(), Error>;
52+
6053
fn str(&mut self, v: &str) -> Result<(), Error>;
54+
fn borrowed_str(&mut self, v: &'v str) -> Result<(), Error> {
55+
self.str(v)
56+
}
57+
6158
fn none(&mut self) -> Result<(), Error>;
6259

6360
#[cfg(feature = "kv_unstable_sval")]
@@ -75,35 +72,45 @@ pub(super) enum Primitive<'v> {
7572
None,
7673
}
7774

75+
impl<'v> Primitive<'v> {
76+
fn visit(self, visitor: &mut dyn Visitor<'v>) -> Result<(), Error> {
77+
match self {
78+
Primitive::Signed(value) => visitor.i64(value),
79+
Primitive::Unsigned(value) => visitor.u64(value),
80+
Primitive::Float(value) => visitor.f64(value),
81+
Primitive::Bool(value) => visitor.bool(value),
82+
Primitive::Char(value) => visitor.char(value),
83+
Primitive::Str(value) => visitor.borrowed_str(value),
84+
Primitive::None => visitor.none(),
85+
}
86+
}
87+
}
88+
7889
mod coerce {
7990
use super::*;
8091

8192
impl<'v> Inner<'v> {
82-
pub(in crate::kv::value) fn as_str(&self) -> Option<&str> {
83-
if let Inner::Primitive(Primitive::Str(value)) = self {
84-
Some(value)
85-
} else {
86-
self.coerce().into_primitive().into_str()
87-
}
93+
pub(in crate::kv::value) fn get_str(&self) -> Option<&str> {
94+
self.coerce().into_primitive().into_str()
8895
}
8996

90-
pub(in crate::kv::value) fn as_u64(&self) -> Option<u64> {
97+
pub(in crate::kv::value) fn get_u64(&self) -> Option<u64> {
9198
self.coerce().into_primitive().into_u64()
9299
}
93100

94-
pub(in crate::kv::value) fn as_i64(&self) -> Option<i64> {
101+
pub(in crate::kv::value) fn get_i64(&self) -> Option<i64> {
95102
self.coerce().into_primitive().into_i64()
96103
}
97104

98-
pub(in crate::kv::value) fn as_f64(&self) -> Option<f64> {
105+
pub(in crate::kv::value) fn get_f64(&self) -> Option<f64> {
99106
self.coerce().into_primitive().into_f64()
100107
}
101108

102-
pub(in crate::kv::value) fn as_char(&self) -> Option<char> {
109+
pub(in crate::kv::value) fn get_char(&self) -> Option<char> {
103110
self.coerce().into_primitive().into_char()
104111
}
105112

106-
pub(in crate::kv::value) fn as_bool(&self) -> Option<bool> {
113+
pub(in crate::kv::value) fn get_bool(&self) -> Option<bool> {
107114
self.coerce().into_primitive().into_bool()
108115
}
109116

@@ -116,7 +123,7 @@ mod coerce {
116123
}
117124
}
118125

119-
impl<'v> Visitor for Coerce<'v> {
126+
impl<'v> Visitor<'v> for Coerce<'v> {
120127
fn debug(&mut self, _: &dyn fmt::Debug) -> Result<(), Error> {
121128
Ok(())
122129
}
@@ -146,8 +153,13 @@ mod coerce {
146153
Ok(())
147154
}
148155

156+
fn borrowed_str(&mut self, v: &'v str) -> Result<(), Error> {
157+
self.0 = Coerced::Primitive(Primitive::Str(v));
158+
Ok(())
159+
}
160+
149161
#[cfg(not(feature = "std"))]
150-
fn str(&mut self, v: &str) -> Result<(), Error> {
162+
fn str(&mut self, _: &str) -> Result<(), Error> {
151163
Ok(())
152164
}
153165

@@ -185,6 +197,7 @@ mod coerce {
185197
fn into_primitive(self) -> Primitive<'v> {
186198
match self {
187199
Coerced::Primitive(value) => value,
200+
#[cfg(feature = "std")]
188201
_ => Primitive::None,
189202
}
190203
}
@@ -247,7 +260,7 @@ mod coerce {
247260
use std::borrow::Cow;
248261

249262
impl<'v> Inner<'v> {
250-
pub(in crate::kv::value) fn to_str(&self) -> Option<Cow<str>> {
263+
pub(in crate::kv::value) fn get_string(&self) -> Option<Cow<str>> {
251264
self.coerce().into_string()
252265
}
253266
}
@@ -289,6 +302,36 @@ mod fmt_support {
289302
}
290303
}
291304

305+
impl<'s, 'f> Slot<'s, 'f> {
306+
/// Fill the slot with a debuggable value.
307+
///
308+
/// The given value doesn't need to satisfy any particular lifetime constraints.
309+
///
310+
/// # Panics
311+
///
312+
/// Calling more than a single `fill` method on this slot will panic.
313+
pub fn fill_debug<T>(&mut self, value: T) -> Result<(), Error>
314+
where
315+
T: fmt::Debug,
316+
{
317+
self.fill(|visitor| visitor.debug(&value))
318+
}
319+
320+
/// Fill the slot with a displayable value.
321+
///
322+
/// The given value doesn't need to satisfy any particular lifetime constraints.
323+
///
324+
/// # Panics
325+
///
326+
/// Calling more than a single `fill` method on this slot will panic.
327+
pub fn fill_display<T>(&mut self, value: T) -> Result<(), Error>
328+
where
329+
T: fmt::Display,
330+
{
331+
self.fill(|visitor| visitor.display(&value))
332+
}
333+
}
334+
292335
impl<'v> fmt::Debug for kv::Value<'v> {
293336
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
294337
self.visit(&mut FmtVisitor(f))?;
@@ -307,7 +350,7 @@ mod fmt_support {
307350

308351
struct FmtVisitor<'a, 'b: 'a>(&'a mut fmt::Formatter<'b>);
309352

310-
impl<'a, 'b: 'a> Visitor for FmtVisitor<'a, 'b> {
353+
impl<'a, 'b: 'a, 'v> Visitor<'v> for FmtVisitor<'a, 'b> {
311354
fn debug(&mut self, v: &dyn fmt::Debug) -> Result<(), Error> {
312355
v.fmt(self.0)?;
313356

@@ -368,6 +411,22 @@ pub(super) mod sval_support {
368411
}
369412
}
370413

414+
impl<'s, 'f> Slot<'s, 'f> {
415+
/// Fill the slot with a structured value.
416+
///
417+
/// The given value doesn't need to satisfy any particular lifetime constraints.
418+
///
419+
/// # Panics
420+
///
421+
/// Calling more than a single `fill` method on this slot will panic.
422+
pub fn fill_sval<T>(&mut self, value: T) -> Result<(), Error>
423+
where
424+
T: sval::Value,
425+
{
426+
self.fill(|visitor| visitor.sval(&value))
427+
}
428+
}
429+
371430
impl<'v> sval::Value for kv::Value<'v> {
372431
fn stream(&self, s: &mut sval::value::Stream) -> sval::value::Result {
373432
self.visit(&mut SvalVisitor(s)).map_err(Error::into_sval)?;
@@ -395,7 +454,7 @@ pub(super) mod sval_support {
395454

396455
struct SvalVisitor<'a, 'b: 'a>(&'a mut sval::value::Stream<'b>);
397456

398-
impl<'a, 'b: 'a> Visitor for SvalVisitor<'a, 'b> {
457+
impl<'a, 'b: 'a, 'v> Visitor<'v> for SvalVisitor<'a, 'b> {
399458
fn debug(&mut self, v: &dyn fmt::Debug) -> Result<(), Error> {
400459
self.0
401460
.fmt(format_args!("{:?}", v))
@@ -496,21 +555,21 @@ pub(super) mod sval_support {
496555
}
497556

498557
#[test]
499-
fn coersion() {
558+
fn sval_coersion() {
500559
assert_eq!(
501560
42u64,
502561
kv::Value::from_sval(&42u64)
503-
.as_u64()
562+
.get_u64()
504563
.expect("invalid value")
505564
);
506565

507-
assert!(kv::Value::from_sval(&"a string").as_str().is_none());
566+
assert!(kv::Value::from_sval(&"a string").get_str().is_none());
508567

509568
#[cfg(feature = "std")]
510569
assert_eq!(
511570
"a string",
512571
&*kv::Value::from_sval(&"a string")
513-
.to_str()
572+
.get_string()
514573
.expect("invalid value")
515574
);
516575
}

0 commit comments

Comments
 (0)