13 use self::internal
::{Inner, Visitor, Primitive}
;
15 /// A type that can be converted into a [`Value`](struct.Value.html).
17 /// Perform the conversion.
18 fn to_value(&self) -> Value
;
21 impl<'a
, T
> ToValue
for &'a T
25 fn to_value(&self) -> Value
{
30 impl<'v
> ToValue
for Value
<'v
> {
31 fn to_value(&self) -> Value
{
38 /// A type that requires extra work to convert into a [`Value`](struct.Value.html).
40 /// This trait is a more advanced initialization API than [`ToValue`](trait.ToValue.html).
41 /// It's intended for erased values coming from other logging frameworks that may need
42 /// to perform extra work to determine the concrete type to use.
45 fn fill(&self, slot
: &mut Slot
) -> Result
<(), Error
>;
48 impl<'a
, T
> Fill
for &'a T
52 fn fill(&self, slot
: &mut Slot
) -> Result
<(), Error
> {
57 /// A value slot to fill using the [`Fill`](trait.Fill.html) trait.
60 visitor
: &'a
mut Visitor
,
63 impl<'a
> fmt
::Debug
for Slot
<'a
> {
64 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
65 f
.debug_struct("Slot").finish()
70 fn new(visitor
: &'a
mut Visitor
) -> Self {
77 /// Fill the slot with a value.
79 /// The given value doesn't need to satisfy any particular lifetime constraints.
83 /// Calling `fill` more than once will panic.
84 pub fn fill(&mut self, value
: Value
) -> Result
<(), Error
> {
85 assert
!(!self.filled
, "the slot has already been filled");
88 value
.visit(self.visitor
)
92 /// A value in a structured key-value pair.
93 pub struct Value
<'v
> {
98 /// Get a value from an internal `Visit`.
99 fn from_primitive(value
: Primitive
<'v
>) -> Self {
101 inner
: Inner
::Primitive(value
),
105 /// Get a value from a debuggable type.
106 pub fn from_debug
<T
>(value
: &'v T
) -> Self
111 inner
: Inner
::Debug(value
),
115 /// Get a value from a displayable type.
116 pub fn from_display
<T
>(value
: &'v T
) -> Self
121 inner
: Inner
::Display(value
),
125 /// Get a value from a fillable slot.
126 pub fn from_fill
<T
>(value
: &'v T
) -> Self
131 inner
: Inner
::Fill(value
),
135 fn visit(&self, visitor
: &mut Visitor
) -> Result
<(), Error
> {
136 self.inner
.visit(visitor
)
140 impl<'v
> fmt
::Debug
for Value
<'v
> {
141 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
142 self.visit(&mut self::internal
::FmtVisitor(f
))?
;
148 impl<'v
> fmt
::Display
for Value
<'v
> {
149 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
150 self.visit(&mut self::internal
::FmtVisitor(f
))?
;
164 impl Fill
for TestFill
{
165 fn fill(&self, slot
: &mut Slot
) -> Result
<(), Error
> {
166 let dbg
: &fmt
::Debug
= &1;
168 slot
.fill(Value
::from_debug(&dbg
))
172 assert_eq
!("1", Value
::from_fill(&TestFill
).to_str_buf());
177 fn fill_multiple_times_panics() {
180 impl Fill
for BadFill
{
181 fn fill(&self, slot
: &mut Slot
) -> Result
<(), Error
> {
182 slot
.fill(42.into
())?
;
183 slot
.fill(6789.into
())?
;
189 let _
= Value
::from_fill(&BadFill
).to_str_buf();