What happens when commit `used` is smaller than granted buffer?
Fabien-Chouteau opened this issue · comments
Hi @jamesmunns,
I have a question about the behavior of commit
when used
is smaller than the granted buffer.
My guess is that the chunk that is not "committed" is available for a future write grand and not available for read
.
Is this correct?
Line 888 in 479eb3d
Hi @Fabien-Chouteau, if you request a grant of say, 64 bytes, then commit 40, then the first 40 bytes of that commit will be made available to the reader, while the next 24 bytes are "recycled" for the next write grant.
Does that make sense?
Yes, it was my understanding from reading the code.
But I'm not sure if the documentation is clear enough on this point.
Maybe it is my lack of Rust knowledge but when I first read the doc it seemed like it was possible to keep using a GrantW
even after a commit:
let mut grant = prod.grant_max_remaining(8).unwrap();
grant.buf().copy_from_slice(&[1, 2, 3, 4]);
grant.commit(4);
grant.buf().copy_from_slice(&[5, 6, 7, 8]);
grant.commit(4);
Which could make sens from a user perspective, take a big chunk of memory but only commit it piece by piece.
Hey, I could make that more clear, but if you look at the docs for GrantW
:
The method signature takes self
by ownership:
pub fn commit(self, used: usize)
// ^^^^
Additionally the docs say this (emphasis mine):
Finalizes a writable grant given by grant() or grant_max(). This makes the data available to be read via read(). This consumes the grant.
In Rust, "consuming" the data means that you will no longer have access to it.
Thanks for the explanation 👍