Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a buffering Stream trait #182

Merged
merged 20 commits into from
Dec 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
split up Stream infra
KodrAus committed Dec 18, 2023
commit 325eac13fc67c476c617446fc1918a153fea670b
1 change: 0 additions & 1 deletion buffer/src/error.rs
Original file line number Diff line number Diff line change
@@ -56,7 +56,6 @@ impl Error {
}

pub(crate) fn invalid_value(reason: &'static str) -> Self {
panic!("{}", reason);
Error(ErrorKind::InvalidValue { reason })
}

7 changes: 6 additions & 1 deletion buffer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -9,9 +9,14 @@ Rather than conditionally compile these methods, this library stubs
out functionality when an allocator isn't available.
*/

//#![cfg_attr(not(test), no_std)]
#![cfg_attr(not(test), no_std)]
#![deny(missing_docs)]

/**
A generic streaming result.
*/
pub type Result<T = (), E = Error> = sval::Result<T, E>;

mod error;

#[cfg(feature = "std")]
1,313 changes: 79 additions & 1,234 deletions buffer/src/stream.rs

Large diffs are not rendered by default.

1,031 changes: 1,031 additions & 0 deletions buffer/src/stream/flat.rs

Large diffs are not rendered by default.

203 changes: 203 additions & 0 deletions buffer/src/stream/flat_enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
use crate::{default_stream, Error, Result, Stream, StreamEnum, Unsupported};

use super::owned_label;

pub(super) struct FlatStreamEnum<S> {
stream: S,
queue: Queue,
}

#[derive(Debug)]
struct NestedVariant {
tag: Option<sval::Tag>,
label: Option<sval::Label<'static>>,
index: Option<sval::Index>,
}

impl<'sval, S: StreamEnum<'sval>> FlatStreamEnum<S> {
pub fn new(stream: S) -> Self {
FlatStreamEnum {
stream,
queue: Default::default(),
}
}

pub fn push(
&mut self,
tag: Option<&sval::Tag>,
label: Option<&sval::Label>,
index: Option<&sval::Index>,
) -> Result {
self.queue.push_back(NestedVariant {
tag: tag.cloned(),
label: if let Some(label) = label {
Some(owned_label(label)?)
} else {
None
},
index: index.cloned(),
})
}

pub fn end(self) -> Result<S::Ok> {
self.value_or_recurse(|stream| stream.end(), |stream| stream.end())
}

fn value_or_recurse(
mut self,
value: impl FnOnce(Self) -> Result<S::Ok>,
nested: impl FnOnce(FlatStreamEnum<S::Nested>) -> Result<<S::Nested as StreamEnum<'sval>>::Ok>,
) -> Result<S::Ok> {
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,
})
},
)
} else {
value(self)
}
}
}

impl<'sval, S: StreamEnum<'sval>> Stream<'sval> for FlatStreamEnum<S> {
type Ok = S::Ok;

type Map = Unsupported<S::Ok>;

type Enum = Unsupported<S::Ok>;

fn value<V: sval::Value + ?Sized>(self, value: &'sval V) -> Result<Self::Ok> {
self.value_or_recurse(
|stream| default_stream::value(stream, value),
|stream| stream.value(value),
)
}

fn value_computed<V: sval::Value + ?Sized>(self, value: &V) -> Result<Self::Ok> {
self.value_or_recurse(
|stream| default_stream::value_computed(stream, value),
|stream| stream.value_computed(value),
)
}

fn null(self) -> Result<Self::Ok> {
Err(Error::invalid_value(
"enum variants must be wrapped in a tag-carrying value",
))
}

fn bool(self, _: bool) -> Result<Self::Ok> {
Err(Error::invalid_value(
"enum variants must be wrapped in a tag-carrying value",
))
}

fn i64(self, _: i64) -> Result<Self::Ok> {
Err(Error::invalid_value(
"enum variants must be wrapped in a tag-carrying value",
))
}

fn f64(self, _: f64) -> Result<Self::Ok> {
Err(Error::invalid_value(
"enum variants must be wrapped in a tag-carrying value",
))
}

fn text_computed(self, _: &str) -> Result<Self::Ok> {
Err(Error::invalid_value(
"enum variants must be wrapped in a tag-carrying value",
))
}

fn tag(
self,
tag: Option<&sval::Tag>,
label: Option<&sval::Label>,
index: Option<&sval::Index>,
) -> Result<Self::Ok> {
self.value_or_recurse(
|stream| stream.stream.tag(tag, label, index),
|stream| Stream::tag(stream, tag, label, index),
)
}

fn tagged<V: sval::Value + ?Sized>(
self,
tag: Option<&sval::Tag>,
label: Option<&sval::Label>,
index: Option<&sval::Index>,
value: &'sval V,
) -> Result<Self::Ok> {
self.value_or_recurse(
|stream| stream.stream.tagged(tag, label, index, value),
|stream| Stream::tagged(stream, tag, label, index, value),
)
}

fn tagged_computed<V: sval::Value + ?Sized>(
self,
tag: Option<&sval::Tag>,
label: Option<&sval::Label>,
index: Option<&sval::Index>,
value: &V,
) -> Result<Self::Ok> {
self.value_or_recurse(
|stream| stream.stream.tagged_computed(tag, label, index, value),
|stream| Stream::tagged_computed(stream, tag, label, index, value),
)
}

fn map_begin(self, _: Option<usize>) -> Result<Self::Map> {
Ok(Unsupported::default())
}

fn enum_begin(
self,
_: Option<&sval::Tag>,
_: Option<&sval::Label>,
_: Option<&sval::Index>,
) -> Result<Self::Enum> {
Ok(Unsupported::default())
}
}

#[derive(Default)]
struct Queue {
#[cfg(feature = "alloc")]
inner: crate::std::collections::VecDeque<NestedVariant>,
#[cfg(not(feature = "alloc"))]
inner: Option<NestedVariant>,
}

impl Queue {
fn push_back(&mut self, variant: NestedVariant) -> Result {
#[cfg(feature = "alloc")]
{
self.inner.push_back(variant);
Ok(())
}
#[cfg(not(feature = "alloc"))]
{
todo!()
}
}

fn pop_front(&mut self) -> Option<NestedVariant> {
#[cfg(feature = "alloc")]
{
self.inner.pop_front()
}
#[cfg(not(feature = "alloc"))]
{
self.inner.take()
}
}
}
5 changes: 0 additions & 5 deletions buffer/src/value.rs
Original file line number Diff line number Diff line change
@@ -777,7 +777,6 @@ impl<'sval> sval::Stream<'sval> for ValueBuf<'sval> {
label: Option<&sval::Label>,
index: Option<&sval::Index>,
) -> sval::Result {
println!("enum_begin");
#[cfg(feature = "alloc")]
{
self.push_begin(ValueKind::Enum {
@@ -802,8 +801,6 @@ impl<'sval> sval::Stream<'sval> for ValueBuf<'sval> {
_: Option<&sval::Label>,
_: Option<&sval::Index>,
) -> sval::Result {
println!("enum_end");

#[cfg(feature = "alloc")]
{
self.try_catch(|buf| buf.push_end())
@@ -844,8 +841,6 @@ impl<'sval> sval::Stream<'sval> for ValueBuf<'sval> {
_: Option<&sval::Label>,
_: Option<&sval::Index>,
) -> sval::Result {
println!("tagged_end");

#[cfg(feature = "alloc")]
{
self.try_catch(|buf| buf.push_end())