diff --git a/buffer/src/stream.rs b/buffer/src/stream.rs index 39c012e6..70c2b7b0 100644 --- a/buffer/src/stream.rs +++ b/buffer/src/stream.rs @@ -137,16 +137,16 @@ pub trait Stream<'sval> { fn tag( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, ) -> Result; fn tagged( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, value: &'sval V, ) -> Result where @@ -157,9 +157,9 @@ pub trait Stream<'sval> { fn tagged_computed( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, value: &V, ) -> Result; @@ -169,25 +169,25 @@ pub trait Stream<'sval> { fn tuple_begin( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, num_entries: Option, ) -> Result; fn record_begin( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, num_entries: Option, ) -> Result; fn enum_begin( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, ) -> Result; } @@ -226,8 +226,8 @@ pub trait StreamTuple<'sval> { fn value( &mut self, - tag: Option<&sval::Tag>, - index: &sval::Index, + tag: Option, + index: sval::Index, value: &'sval V, ) -> Result { default_stream::tuple_value(self, tag, index, value) @@ -235,8 +235,8 @@ pub trait StreamTuple<'sval> { fn value_computed( &mut self, - tag: Option<&sval::Tag>, - index: &sval::Index, + tag: Option, + index: sval::Index, value: &V, ) -> Result; @@ -248,8 +248,8 @@ pub trait StreamRecord<'sval> { fn value( &mut self, - tag: Option<&sval::Tag>, - label: &sval::Label, + tag: Option, + label: sval::Label, value: &'sval V, ) -> Result { default_stream::record_value(self, tag, label, value) @@ -257,8 +257,8 @@ pub trait StreamRecord<'sval> { fn value_computed( &mut self, - tag: Option<&sval::Tag>, - label: &sval::Label, + tag: Option, + label: sval::Label, value: &V, ) -> Result; @@ -274,16 +274,16 @@ pub trait StreamEnum<'sval> { fn tag( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, ) -> Result; fn tagged( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, value: &'sval V, ) -> Result where @@ -294,33 +294,33 @@ pub trait StreamEnum<'sval> { fn tagged_computed( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, value: &V, ) -> Result; fn tuple_begin( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, num_entries: Option, ) -> Result; fn record_begin( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, num_entries: Option, ) -> Result; fn nested Result<>::Ok>>( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, variant: F, ) -> Result; @@ -366,18 +366,18 @@ impl<'sval, Ok> Stream<'sval> for Unsupported { fn tag( self, - _: Option<&sval::Tag>, - _: Option<&sval::Label>, - _: Option<&sval::Index>, + _: Option, + _: Option, + _: Option, ) -> Result { Err(Error::invalid_value("tags are unsupported")) } fn tagged_computed( self, - _: Option<&sval::Tag>, - _: Option<&sval::Label>, - _: Option<&sval::Index>, + _: Option, + _: Option, + _: Option, _: &V, ) -> Result { Err(Error::invalid_value("tagged values are unsupported")) @@ -393,9 +393,9 @@ impl<'sval, Ok> Stream<'sval> for Unsupported { fn tuple_begin( self, - _: Option<&sval::Tag>, - _: Option<&sval::Label>, - _: Option<&sval::Index>, + _: Option, + _: Option, + _: Option, _: Option, ) -> Result { Err(Error::invalid_value("records are unsupported")) @@ -403,9 +403,9 @@ impl<'sval, Ok> Stream<'sval> for Unsupported { fn record_begin( self, - _: Option<&sval::Tag>, - _: Option<&sval::Label>, - _: Option<&sval::Index>, + _: Option, + _: Option, + _: Option, _: Option, ) -> Result { Err(Error::invalid_value("records are unsupported")) @@ -413,9 +413,9 @@ impl<'sval, Ok> Stream<'sval> for Unsupported { fn enum_begin( self, - _: Option<&sval::Tag>, - _: Option<&sval::Label>, - _: Option<&sval::Index>, + _: Option, + _: Option, + _: Option, ) -> Result { Err(Error::invalid_value("enums are unsupported")) } @@ -454,8 +454,8 @@ impl<'sval, Ok> StreamTuple<'sval> for Unsupported { fn value_computed( &mut self, - _: Option<&sval::Tag>, - _: &sval::Index, + _: Option, + _: sval::Index, _: &V, ) -> Result { Err(Error::invalid_value("tuples are unsupported")) @@ -471,8 +471,8 @@ impl<'sval, Ok> StreamRecord<'sval> for Unsupported { fn value_computed( &mut self, - _: Option<&sval::Tag>, - _: &sval::Label, + _: Option, + _: sval::Label, _: &V, ) -> Result { Err(Error::invalid_value("records are unsupported")) @@ -492,18 +492,18 @@ impl<'sval, Ok> StreamEnum<'sval> for Unsupported { fn tag( self, - _: Option<&sval::Tag>, - _: Option<&sval::Label>, - _: Option<&sval::Index>, + _: Option, + _: Option, + _: Option, ) -> Result { Err(Error::invalid_value("enums are unsupported")) } fn tagged_computed( self, - _: Option<&sval::Tag>, - _: Option<&sval::Label>, - _: Option<&sval::Index>, + _: Option, + _: Option, + _: Option, _: &V, ) -> Result { Err(Error::invalid_value("enums are unsupported")) @@ -511,9 +511,9 @@ impl<'sval, Ok> StreamEnum<'sval> for Unsupported { fn tuple_begin( self, - _: Option<&sval::Tag>, - _: Option<&sval::Label>, - _: Option<&sval::Index>, + _: Option, + _: Option, + _: Option, _: Option, ) -> Result { Err(Error::invalid_value("enums are unsupported")) @@ -521,9 +521,9 @@ impl<'sval, Ok> StreamEnum<'sval> for Unsupported { fn record_begin( self, - _: Option<&sval::Tag>, - _: Option<&sval::Label>, - _: Option<&sval::Index>, + _: Option, + _: Option, + _: Option, _: Option, ) -> Result { Err(Error::invalid_value("enums are unsupported")) @@ -531,9 +531,9 @@ impl<'sval, Ok> StreamEnum<'sval> for Unsupported { fn nested Result<>::Ok>>( self, - _: Option<&sval::Tag>, - _: Option<&sval::Label>, - _: Option<&sval::Index>, + _: Option, + _: Option, + _: Option, _: F, ) -> Result { Err(Error::invalid_value("enums are unsupported")) @@ -544,7 +544,11 @@ impl<'sval, Ok> StreamEnum<'sval> for Unsupported { } } -fn owned_label(label: &sval::Label) -> Result> { +fn owned_label(label: sval::Label) -> Result> { + owned_label_ref(&label) +} + +fn owned_label_ref(label: &sval::Label) -> Result> { #[cfg(feature = "alloc")] { Ok(label.to_owned()) @@ -673,8 +677,8 @@ pub mod default_stream { pub fn tuple_value<'sval, S: StreamTuple<'sval> + ?Sized, V: sval::Value + ?Sized>( tuple: &mut S, - tag: Option<&sval::Tag>, - index: &sval::Index, + tag: Option, + index: sval::Index, value: &'sval V, ) -> Result { tuple.value_computed(tag, index, value) @@ -682,8 +686,8 @@ pub mod default_stream { pub fn record_value<'sval, S: StreamRecord<'sval> + ?Sized, V: sval::Value + ?Sized>( record: &mut S, - tag: Option<&sval::Tag>, - label: &sval::Label, + tag: Option, + label: sval::Label, value: &'sval V, ) -> Result { record.value_computed(tag, label, value) @@ -691,9 +695,9 @@ pub mod default_stream { pub fn enum_tagged<'sval, S: StreamEnum<'sval>, V: sval::Value + ?Sized>( stream: S, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, value: &'sval V, ) -> Result { stream.tagged_computed(tag, label, index, value) @@ -729,13 +733,13 @@ mod tests { assert_eq!( Value::Record(Record { - tag: Tag::new(None, Some(&sval::Label::new("DeriveRecord")), None).unwrap(), + tag: Tag::new(None, Some(sval::Label::new("DeriveRecord")), None).unwrap(), entries: vec![ (sval::Label::new("a"), Value::I64(1)), ( sval::Label::new("b"), Value::Tuple(Tuple { - tag: Tag::new(None, Some(&sval::Label::new("DeriveTuple")), None) + tag: Tag::new(None, Some(sval::Label::new("DeriveTuple")), None) .unwrap(), entries: vec![ (sval::Index::new(0), Value::I64(2)), @@ -744,15 +748,15 @@ mod tests { Value::Enum(Enum { tag: Tag::new( None, - Some(&sval::Label::new("DeriveEnum")), + Some(sval::Label::new("DeriveEnum")), None ) .unwrap(), variant: Some(Variant::Record(Record { tag: Tag::new( None, - Some(&sval::Label::new("Record")), - Some(&sval::Index::new(0)) + Some(sval::Label::new("Record")), + Some(sval::Index::new(0)) ) .unwrap(), entries: vec![ @@ -762,7 +766,7 @@ mod tests { Value::Tagged(Tagged { tag: Tag::new( None, - Some(&sval::Label::new("DeriveTagged")), + Some(sval::Label::new("DeriveTagged")), None ) .unwrap(), @@ -849,7 +853,7 @@ mod tests { fn stream_tuple() { assert_eq!( Value::Tuple(Tuple { - tag: Tag::new(None, Some(&sval::Label::new("Tuple")), None).unwrap(), + tag: Tag::new(None, Some(sval::Label::new("Tuple")), None).unwrap(), entries: vec![ (sval::Index::new(0), Value::I64(1)), (sval::Index::new(1), Value::Bool(true)), @@ -893,12 +897,12 @@ mod tests { fn stream_enum_tuple_variant() { assert_eq!( Value::Enum(Enum { - tag: Tag::new(None, Some(&sval::Label::new("Enum")), None).unwrap(), + tag: Tag::new(None, Some(sval::Label::new("Enum")), None).unwrap(), variant: Some(Variant::Tuple(Tuple { tag: Tag::new( None, - Some(&sval::Label::new("Tuple")), - Some(&sval::Index::new(0)) + Some(sval::Label::new("Tuple")), + Some(sval::Index::new(0)) ) .unwrap(), entries: vec![ @@ -953,7 +957,7 @@ mod tests { fn stream_record() { assert_eq!( Value::Record(Record { - tag: Tag::new(None, Some(&sval::Label::new("Record")), None).unwrap(), + tag: Tag::new(None, Some(sval::Label::new("Record")), None).unwrap(), entries: vec![ (sval::Label::new("a"), Value::I64(1)), (sval::Label::new("b"), Value::Bool(true)), @@ -997,12 +1001,12 @@ mod tests { fn stream_enum_record_variant() { assert_eq!( Value::Enum(Enum { - tag: Tag::new(None, Some(&sval::Label::new("Enum")), None).unwrap(), + tag: Tag::new(None, Some(sval::Label::new("Enum")), None).unwrap(), variant: Some(Variant::Record(Record { tag: Tag::new( None, - Some(&sval::Label::new("Record")), - Some(&sval::Index::new(0)) + Some(sval::Label::new("Record")), + Some(sval::Index::new(0)) ) .unwrap(), entries: vec![ @@ -1144,11 +1148,11 @@ mod tests { assert_eq!( Value::Enum(Enum { - tag: Tag::new(None, Some(&sval::Label::new("Layer1")), None).unwrap(), + tag: Tag::new(None, Some(sval::Label::new("Layer1")), None).unwrap(), variant: Some(Variant::Enum(Box::new(Enum { - tag: Tag::new(None, Some(&sval::Label::new("Layer2")), None).unwrap(), + tag: Tag::new(None, Some(sval::Label::new("Layer2")), None).unwrap(), variant: Some(Variant::Tagged(Tagged { - tag: Tag::new(None, Some(&sval::Label::new("Value")), None).unwrap(), + tag: Tag::new(None, Some(sval::Label::new("Value")), None).unwrap(), value: Box::new(Value::I64(42)), })) }))) @@ -1189,30 +1193,26 @@ mod tests { assert_eq!( Value::Enum(Enum { - tag: Tag::new(None, Some(&sval::Label::new("Layer1")), None).unwrap(), + tag: Tag::new(None, Some(sval::Label::new("Layer1")), None).unwrap(), variant: Some(Variant::Enum(Box::new(Enum { - tag: Tag::new(None, Some(&sval::Label::new("Layer2")), None).unwrap(), + tag: Tag::new(None, Some(sval::Label::new("Layer2")), None).unwrap(), variant: Some(Variant::Enum(Box::new(Enum { - tag: Tag::new(None, Some(&sval::Label::new("Layer3")), None).unwrap(), + tag: Tag::new(None, Some(sval::Label::new("Layer3")), None).unwrap(), variant: Some(Variant::Enum(Box::new(Enum { - tag: Tag::new(None, Some(&sval::Label::new("Layer4")), None).unwrap(), + tag: Tag::new(None, Some(sval::Label::new("Layer4")), None).unwrap(), variant: Some(Variant::Enum(Box::new(Enum { - tag: Tag::new(None, Some(&sval::Label::new("Layer5")), None) + tag: Tag::new(None, Some(sval::Label::new("Layer5")), None) .unwrap(), variant: Some(Variant::Enum(Box::new(Enum { - tag: Tag::new(None, Some(&sval::Label::new("Layer6")), None) + tag: Tag::new(None, Some(sval::Label::new("Layer6")), None) .unwrap(), variant: Some(Variant::Enum(Box::new(Enum { - tag: Tag::new( - None, - Some(&sval::Label::new("Layer7")), - None - ) - .unwrap(), + tag: Tag::new(None, Some(sval::Label::new("Layer7")), None) + .unwrap(), variant: Some(Variant::Tagged(Tagged { tag: Tag::new( None, - Some(&sval::Label::new("Value")), + Some(sval::Label::new("Value")), None ) .unwrap(), @@ -1268,14 +1268,14 @@ mod tests { impl Tag { fn new( - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, ) -> Result { Ok(Tag { - tag: tag.cloned(), + tag, label: label.map(owned_label).transpose()?, - index: index.cloned(), + index: index, }) } } @@ -1397,9 +1397,9 @@ mod tests { fn tag( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, ) -> Result { let tag = Tag::new(tag, label, index)?; @@ -1408,9 +1408,9 @@ mod tests { fn tagged_computed( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, value: &V, ) -> Result { let tag = Tag::new(tag, label, index)?; @@ -1441,9 +1441,9 @@ mod tests { fn tuple_begin( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, _: Option, ) -> Result { Ok(ToTuple { @@ -1456,9 +1456,9 @@ mod tests { fn record_begin( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, _: Option, ) -> Result { Ok(ToRecord { @@ -1471,9 +1471,9 @@ mod tests { fn enum_begin( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, ) -> Result { Ok(ToEnum { tag: Tag::new(tag, label, index)?, @@ -1549,8 +1549,8 @@ mod tests { fn value( &mut self, - _: Option<&sval::Tag>, - index: &sval::Index, + _: Option, + index: sval::Index, value: &'sval V, ) -> Result { let value = ToValue::default().value(value)?; @@ -1562,8 +1562,8 @@ mod tests { fn value_computed( &mut self, - _: Option<&sval::Tag>, - index: &sval::Index, + _: Option, + index: sval::Index, value: &V, ) -> Result { let value = ToValue::default().value_computed(value)?; @@ -1583,8 +1583,8 @@ mod tests { fn value( &mut self, - _: Option<&sval::Tag>, - label: &sval::Label, + _: Option, + label: sval::Label, value: &'sval V, ) -> Result { let label = owned_label(label)?; @@ -1597,8 +1597,8 @@ mod tests { fn value_computed( &mut self, - _: Option<&sval::Tag>, - label: &sval::Label, + _: Option, + label: sval::Label, value: &V, ) -> Result { let label = owned_label(label)?; @@ -1623,9 +1623,9 @@ mod tests { fn tag( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, ) -> Result { let tag = Tag::new(tag, label, index)?; @@ -1637,9 +1637,9 @@ mod tests { fn tagged_computed( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, value: &V, ) -> Result { let tag = Tag::new(tag, label, index)?; @@ -1656,9 +1656,9 @@ mod tests { fn tuple_begin( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, _: Option, ) -> Result { Ok(ToVariant { @@ -1674,9 +1674,9 @@ mod tests { fn record_begin( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, _: Option, ) -> Result { Ok(ToVariant { @@ -1692,9 +1692,9 @@ mod tests { fn nested Result<>::Ok>>( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, variant: F, ) -> Result { let variant = variant(ToEnum { @@ -1722,8 +1722,8 @@ mod tests { fn value( &mut self, - tag: Option<&sval::Tag>, - index: &sval::Index, + tag: Option, + index: sval::Index, value: &'sval V, ) -> Result { self.stream.value(tag, index, value) @@ -1731,8 +1731,8 @@ mod tests { fn value_computed( &mut self, - tag: Option<&sval::Tag>, - index: &sval::Index, + tag: Option, + index: sval::Index, value: &V, ) -> Result { self.stream.value_computed(tag, index, value) @@ -1751,8 +1751,8 @@ mod tests { fn value( &mut self, - tag: Option<&sval::Tag>, - label: &sval::Label, + tag: Option, + label: sval::Label, value: &'sval V, ) -> Result { self.stream.value(tag, label, value) @@ -1760,8 +1760,8 @@ mod tests { fn value_computed( &mut self, - tag: Option<&sval::Tag>, - label: &sval::Label, + tag: Option, + label: sval::Label, value: &V, ) -> Result { self.stream.value_computed(tag, label, value) diff --git a/buffer/src/stream/flat.rs b/buffer/src/stream/flat.rs index 57d2b93d..e820d93d 100644 --- a/buffer/src/stream/flat.rs +++ b/buffer/src/stream/flat.rs @@ -5,7 +5,7 @@ use crate::{ TextBuf, ValueBuf, }; -use super::{flat_enum::FlatStreamEnum, owned_label}; +use super::{flat_enum::FlatStreamEnum, owned_label_ref}; pub(super) struct FlatStream<'sval, S: Stream<'sval>> { buffered: Option>, @@ -300,11 +300,17 @@ impl<'sval, S: Stream<'sval>> sval::Stream<'sval> for FlatStream<'sval, S> { |buf| buf.enum_begin(tag, label, index), |stream| { Ok(State::Enum(Some(Enum { - stream: FlatStreamEnum::new(stream.stream.enum_begin(tag, label, index)?), + stream: FlatStreamEnum::new(stream.stream.enum_begin( + tag.cloned(), + label.cloned(), + index.cloned(), + )?), }))) }, |mut stream| { - stream.stream.push(tag, label, index)?; + stream + .stream + .push(tag.cloned(), label.cloned(), index.cloned())?; Ok(State::Enum(Some(stream))) }, @@ -342,7 +348,7 @@ impl<'sval, S: Stream<'sval>> sval::Stream<'sval> for FlatStream<'sval, S> { stream: stream.stream, tag: tag.cloned(), label: if let Some(label) = label { - Some(owned_label(label)?) + Some(owned_label_ref(label)?) } else { None }, @@ -354,7 +360,7 @@ impl<'sval, S: Stream<'sval>> sval::Stream<'sval> for FlatStream<'sval, S> { stream: stream.stream, tag: tag.cloned(), label: if let Some(label) = label { - Some(owned_label(label)?) + Some(owned_label_ref(label)?) } else { None }, @@ -387,13 +393,23 @@ impl<'sval, S: Stream<'sval>> sval::Stream<'sval> for FlatStream<'sval, S> { |buf| buf.record_begin(tag, label, index, num_entries), |stream| { Ok(State::Record(Some(Record { - stream: stream.stream.record_begin(tag, label, index, num_entries)?, + stream: stream.stream.record_begin( + tag.cloned(), + label.cloned(), + index.cloned(), + num_entries, + )?, field: None, }))) }, |stream| { Ok(State::EnumVariant(Some(EnumVariant::Record(Record { - stream: stream.stream.record_begin(tag, label, index, num_entries)?, + stream: stream.stream.record_begin( + tag.cloned(), + label.cloned(), + index.cloned(), + num_entries, + )?, field: None, })))) }, @@ -406,12 +422,12 @@ impl<'sval, S: Stream<'sval>> sval::Stream<'sval> for FlatStream<'sval, S> { |stream| { stream.with_record( |record| { - record.field = Some((tag.cloned(), owned_label(label)?)); + record.field = Some((tag.cloned(), owned_label_ref(label)?)); Ok(()) }, |record_variant| { - record_variant.field = Some((tag.cloned(), owned_label(label)?)); + record_variant.field = Some((tag.cloned(), owned_label_ref(label)?)); Ok(()) }, @@ -452,13 +468,23 @@ impl<'sval, S: Stream<'sval>> sval::Stream<'sval> for FlatStream<'sval, S> { |buf| buf.tuple_begin(tag, label, index, num_entries), |stream| { Ok(State::Tuple(Some(Tuple { - stream: stream.stream.tuple_begin(tag, label, index, num_entries)?, + stream: stream.stream.tuple_begin( + tag.cloned(), + label.cloned(), + index.cloned(), + num_entries, + )?, field: None, }))) }, |stream| { Ok(State::EnumVariant(Some(EnumVariant::Tuple(Tuple { - stream: stream.stream.tuple_begin(tag, label, index, num_entries)?, + stream: stream.stream.tuple_begin( + tag.cloned(), + label.cloned(), + index.cloned(), + num_entries, + )?, field: None, })))) }, @@ -518,7 +544,7 @@ impl<'sval, S: Stream<'sval>> sval::Stream<'sval> for FlatStream<'sval, S> { stream .state .value_computed(&Tag(tag, label, index), |stream, _| { - stream.tag(tag, label, index) + stream.tag(tag.cloned(), label.cloned(), index.cloned()) }) }, ) @@ -815,30 +841,30 @@ impl<'sval, S: Stream<'sval>> State<'sval, S> { any: impl FnOnce(S) -> Result, tagged: impl FnOnce( S, - Option<&sval::Tag>, - Option<&sval::Label>, - Option<&sval::Index>, + Option, + Option, + Option, ) -> Result, seq: impl FnOnce(&mut S::Seq) -> Result, map_key: impl FnOnce(&mut S::Map) -> Result, map_value: impl FnOnce(&mut S::Map) -> Result, - tuple: impl FnOnce(&mut S::Tuple, Option<&sval::Tag>, &sval::Index) -> Result, - record: impl FnOnce(&mut S::Record, Option<&sval::Tag>, &sval::Label) -> Result, + tuple: impl FnOnce(&mut S::Tuple, Option, sval::Index) -> Result, + record: impl FnOnce(&mut S::Record, Option, sval::Label) -> Result, tagged_variant: impl FnOnce( FlatStreamEnum, - Option<&sval::Tag>, - Option<&sval::Label>, - Option<&sval::Index>, + Option, + Option, + Option, ) -> Result, tuple_variant: impl FnOnce( &mut >::Tuple, - Option<&sval::Tag>, - &sval::Index, + Option, + sval::Index, ) -> Result, record_variant: impl FnOnce( &mut >::Record, - Option<&sval::Tag>, - &sval::Label, + Option, + sval::Label, ) -> Result, ) -> Result> { match self { @@ -858,9 +884,9 @@ impl<'sval, S: Stream<'sval>> State<'sval, S> { Ok(Some(tagged( stream.stream, - stream.tag.as_ref(), - stream.label.as_ref(), - stream.index.as_ref(), + stream.tag, + stream.label, + stream.index, )?)) } State::Seq(stream) => { @@ -892,11 +918,11 @@ impl<'sval, S: Stream<'sval>> State<'sval, S> { Error::invalid_value("cannot stream a tuple; the stream is already completed") })?; - let (tag, index) = stream.field.as_ref().ok_or_else(|| { + let (tag, index) = stream.field.take().ok_or_else(|| { Error::invalid_value("cannot stream a tuple; the field index is missing") })?; - tuple(&mut stream.stream, tag.as_ref(), index)?; + tuple(&mut stream.stream, tag, index)?; Ok(None) } @@ -905,11 +931,11 @@ impl<'sval, S: Stream<'sval>> State<'sval, S> { Error::invalid_value("cannot stream a record; the stream is already completed") })?; - let (tag, label) = stream.field.as_ref().ok_or_else(|| { + let (tag, label) = stream.field.take().ok_or_else(|| { Error::invalid_value("cannot stream a record; the field label is missing") })?; - record(&mut stream.stream, tag.as_ref(), label)?; + record(&mut stream.stream, tag, label)?; Ok(None) } @@ -921,13 +947,13 @@ impl<'sval, S: Stream<'sval>> State<'sval, S> { ref mut stream, ref mut field, })) => { - let (tag, index) = field.as_ref().ok_or_else(|| { + let (tag, index) = field.take().ok_or_else(|| { Error::invalid_value( "cannot stream a tuple variant; the field index is missing", ) })?; - tuple_variant(stream, tag.as_ref(), index)?; + tuple_variant(stream, tag, index)?; Ok(None) } @@ -935,13 +961,13 @@ impl<'sval, S: Stream<'sval>> State<'sval, S> { ref mut stream, ref mut field, })) => { - let (tag, label) = field.as_ref().ok_or_else(|| { + let (tag, label) = field.take().ok_or_else(|| { Error::invalid_value( "cannot stream a record variant; the field label is missing", ) })?; - record_variant(stream, tag.as_ref(), label)?; + record_variant(stream, tag, label)?; Ok(None) } @@ -956,12 +982,7 @@ impl<'sval, S: Stream<'sval>> State<'sval, S> { tag, label, index, - }) => Ok(Some(tagged_variant( - stream, - tag.as_ref(), - label.as_ref(), - index.as_ref(), - )?)), + }) => Ok(Some(tagged_variant(stream, tag, label, index)?)), _ => unreachable!(), } } diff --git a/buffer/src/stream/flat_enum.rs b/buffer/src/stream/flat_enum.rs index 435515e8..7fe415d8 100644 --- a/buffer/src/stream/flat_enum.rs +++ b/buffer/src/stream/flat_enum.rs @@ -24,44 +24,47 @@ impl<'sval, S: StreamEnum<'sval>> FlatStreamEnum { pub fn push( &mut self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, ) -> Result { self.queue.push_back(NestedVariant { - tag: tag.cloned(), + tag, label: if let Some(label) = label { Some(owned_label(label)?) } else { None }, - index: index.cloned(), + index, }) } pub fn end(self) -> Result { - self.value_or_recurse(|stream| stream.end(), |stream| stream.end()) + self.value_or_recurse(|stream, _| stream.end(), |stream, _| stream.end(), ()) } - fn value_or_recurse( + fn value_or_recurse( mut self, - value: impl FnOnce(Self) -> Result, - nested: impl FnOnce(FlatStreamEnum) -> Result<>::Ok>, + value: impl FnOnce(Self, I) -> Result, + nested: impl FnOnce( + FlatStreamEnum, + I, + ) -> Result<>::Ok>, + input: I, ) -> Result { if let Some(variant) = self.queue.pop_front() { - self.stream.nested( - variant.tag.as_ref(), - variant.label.as_ref(), - variant.index.as_ref(), - |variant| { - nested(FlatStreamEnum { - stream: variant, - queue: self.queue, - }) - }, - ) + self.stream + .nested(variant.tag, variant.label, variant.index, |variant| { + nested( + FlatStreamEnum { + stream: variant, + queue: self.queue, + }, + input, + ) + }) } else { - value(self) + value(self, input) } } } @@ -79,15 +82,17 @@ impl<'sval, S: StreamEnum<'sval>> Stream<'sval> for FlatStreamEnum { fn value(self, value: &'sval V) -> Result { self.value_or_recurse( - |stream| default_stream::value(stream, value), - |stream| stream.value(value), + |stream, _| default_stream::value(stream, value), + |stream, _| stream.value(value), + (), ) } fn value_computed(self, value: &V) -> Result { self.value_or_recurse( - |stream| default_stream::value_computed(stream, value), - |stream| stream.value_computed(value), + |stream, _| default_stream::value_computed(stream, value), + |stream, _| stream.value_computed(value), + (), ) } @@ -123,39 +128,42 @@ impl<'sval, S: StreamEnum<'sval>> Stream<'sval> for FlatStreamEnum { fn tag( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, ) -> Result { self.value_or_recurse( - |stream| stream.stream.tag(tag, label, index), - |stream| Stream::tag(stream, tag, label, index), + |stream, (tag, label, index)| stream.stream.tag(tag, label, index), + |stream, (tag, label, index)| Stream::tag(stream, tag, label, index), + (tag, label, index), ) } fn tagged( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, value: &'sval V, ) -> Result { self.value_or_recurse( - |stream| stream.stream.tagged(tag, label, index, value), - |stream| Stream::tagged(stream, tag, label, index, value), + |stream, (tag, label, index)| stream.stream.tagged(tag, label, index, value), + |stream, (tag, label, index)| Stream::tagged(stream, tag, label, index, value), + (tag, label, index), ) } fn tagged_computed( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, value: &V, ) -> Result { self.value_or_recurse( - |stream| stream.stream.tagged_computed(tag, label, index, value), - |stream| Stream::tagged_computed(stream, tag, label, index, value), + |stream, (tag, label, index)| stream.stream.tagged_computed(tag, label, index, value), + |stream, (tag, label, index)| Stream::tagged_computed(stream, tag, label, index, value), + (tag, label, index), ) } @@ -169,9 +177,9 @@ impl<'sval, S: StreamEnum<'sval>> Stream<'sval> for FlatStreamEnum { fn tuple_begin( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, num_entries: Option, ) -> Result { assert!(self.queue.is_empty()); @@ -181,9 +189,9 @@ impl<'sval, S: StreamEnum<'sval>> Stream<'sval> for FlatStreamEnum { fn record_begin( self, - tag: Option<&sval::Tag>, - label: Option<&sval::Label>, - index: Option<&sval::Index>, + tag: Option, + label: Option, + index: Option, num_entries: Option, ) -> Result { assert!(self.queue.is_empty()); @@ -193,9 +201,9 @@ impl<'sval, S: StreamEnum<'sval>> Stream<'sval> for FlatStreamEnum { fn enum_begin( self, - _: Option<&sval::Tag>, - _: Option<&sval::Label>, - _: Option<&sval::Index>, + _: Option, + _: Option, + _: Option, ) -> Result { unreachable!() }