-
Notifications
You must be signed in to change notification settings - Fork 13
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
back_to_the_buffer
cannot easily be used recursively
#36
Comments
The above could be turned into a helper function, which would bring things back towards the docs: fn gen_bttb<W: Write, F: SerializeFn<W>>(
f: F,
w: WriteContext<W>,
) -> Result<(WriteContext<W>, u64), GenError> {
let begin = w.position;
f(w).map(|wctx| {
let pos = wctx.position;
(wctx, pos - begin)
})
} back_to_the_buffer(
4,
move |buf| gen_bttb(string("test"), buf),
move |buf, len| gen_simple(be_u32(len as u32), buf)
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As the title says,
back_to_the_buffer
can not easily be used from within anotherback_to_the_buffer
call, which makes it hard to serialize some nested formats.The default/recommended way to use back_to_the_buffer would be to use
gen(<serializer>)
inside thegen
closure like in the docs.However, this wraps
buf
in a secondWriteContext
, so that the serializer is called with aWriteContext<WriteContext<W: Write + BackToTheBuffer>>
.With this, we can no longer call
back_to_the_buffer
again, since it requires aWriteContext<W: Write + BackToTheBuffer>
.It took me quite a while to come up with this solution: (adapted to the doc example)
Now
back_to_the_buffer
could be called again in thestring
combinator.Could this be fixed/made easier in a nice way?
If not, maybe this example could at least be added to the docs so that others don't run into the same wall as I did.
Thanks!
The text was updated successfully, but these errors were encountered: