Skip to content

Commit

Permalink
refactor value layout for easier additional frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Jul 16, 2019
1 parent e5bfc54 commit 15ccf07
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 61 deletions.
92 changes: 67 additions & 25 deletions src/kv/value/internal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;

use super::{Fill, Slot, Error};
use kv;

// `Visit` and `Visitor` is an internal API for visiting the structure of a value.
// It's not intended to be public (at this stage).
Expand Down Expand Up @@ -68,41 +69,82 @@ pub(super) enum Primitive<'v> {
None,
}

/// A visitor for `std::fmt`.
pub(super) struct FmtVisitor<'a, 'b: 'a>(pub(super) &'a mut fmt::Formatter<'b>);

impl<'a, 'b: 'a> Visitor for FmtVisitor<'a, 'b> {
fn debug(&mut self, v: &fmt::Debug) -> Result<(), Error> {
v.fmt(self.0)?;
mod fmt_support {
use super::*;

impl<'v> kv::Value<'v> {
/// Get a value from a debuggable type.
pub fn from_debug<T>(value: &'v T) -> Self
where
T: fmt::Debug,
{
kv::Value {
inner: Inner::Debug(value),
}
}

Ok(())
/// Get a value from a displayable type.
pub fn from_display<T>(value: &'v T) -> Self
where
T: fmt::Display,
{
kv::Value {
inner: Inner::Display(value),
}
}
}

fn u64(&mut self, v: u64) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
}
impl<'v> fmt::Debug for kv::Value<'v> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.visit(&mut FmtVisitor(f))?;

fn i64(&mut self, v: i64) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
Ok(())
}
}

fn f64(&mut self, v: f64) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
}
impl<'v> fmt::Display for kv::Value<'v> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.visit(&mut FmtVisitor(f))?;

fn bool(&mut self, v: bool) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
Ok(())
}
}

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

fn char(&mut self, v: char) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
}
impl<'a, 'b: 'a> Visitor for FmtVisitor<'a, 'b> {
fn debug(&mut self, v: &fmt::Debug) -> Result<(), Error> {
v.fmt(self.0)?;

fn str(&mut self, v: &str) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
}
Ok(())
}

fn u64(&mut self, v: u64) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
}

fn none(&mut self) -> Result<(), Error> {
self.debug(&format_args!("None"))
fn i64(&mut self, v: i64) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
}

fn f64(&mut self, v: f64) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
}

fn bool(&mut self, v: bool) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
}

fn char(&mut self, v: char) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
}

fn str(&mut self, v: &str) -> Result<(), Error> {
self.debug(&format_args!("{:?}", v))
}

fn none(&mut self) -> Result<(), Error> {
self.debug(&format_args!("None"))
}
}
}
36 changes: 0 additions & 36 deletions src/kv/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,6 @@ impl<'v> Value<'v> {
}
}

/// Get a value from a debuggable type.
pub fn from_debug<T>(value: &'v T) -> Self
where
T: fmt::Debug,
{
Value {
inner: Inner::Debug(value),
}
}

/// Get a value from a displayable type.
pub fn from_display<T>(value: &'v T) -> Self
where
T: fmt::Display,
{
Value {
inner: Inner::Display(value),
}
}

/// Get a value from a fillable slot.
pub fn from_fill<T>(value: &'v T) -> Self
where
Expand All @@ -137,22 +117,6 @@ impl<'v> Value<'v> {
}
}

impl<'v> fmt::Debug for Value<'v> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.visit(&mut self::internal::FmtVisitor(f))?;

Ok(())
}
}

impl<'v> fmt::Display for Value<'v> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.visit(&mut self::internal::FmtVisitor(f))?;

Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 15ccf07

Please sign in to comment.