pub struct ZIO { /* private fields */ }Expand description
Buffered input stream wrapping an external chunk-reader callback.
Corresponds to struct Zio / ZIO in lzio.h. The C struct stored a
lua_State *L back-pointer and a void *data opaque pointer alongside a
raw lua_Reader function pointer. In Rust:
lua_State *Lis removed from the struct; callers hold&mut LuaStatedirectly and pass it to fallible methods.void *datais folded into the reader closure.const char *p(raw pointer into the reader’s internal buffer) becomes ausizeindex into the ownedcurrent_chunkfield.
Implementations§
Source§impl ZIO
impl ZIO
Sourcepub fn new(reader: ChunkReader) -> Self
pub fn new(reader: ChunkReader) -> Self
Initialise a ZIO with the given reentrant reader callback.
Corresponds to luaZ_init in lzio.c. The C parameters reader and
data are combined into a single closure; L is threaded through the
fallible methods rather than stored on the struct.
Sourcepub fn from_bytes(bytes: Vec<u8>) -> Self
pub fn from_bytes(bytes: Vec<u8>) -> Self
Construct a ZIO that yields the supplied bytes once and then EOZ.
Used for in-memory sources (a string chunk, or the lexer’s own unit
tests) where there is no reader to call back into Lua. The state passed
to getc/fill is ignored by this reader.
Sourcepub fn take(&mut self) -> ZIO
pub fn take(&mut self) -> ZIO
Move this stream out, leaving an exhausted (empty) ZIO in its place.
The parser owns the lexer’s LexState, which owns its ZIO; the loader
only holds a &mut ZIO. This hands the live stream — its reader and any
bytes already buffered by [getc] — to the parser by value so the lexer
can keep pulling from the same reader (and the same &mut LuaState)
on demand. The original slot becomes an immediately-EOZ stream.
Sourcepub fn getc(&mut self, state: &mut LuaState) -> Result<i32, LuaError>
pub fn getc(&mut self, state: &mut LuaState) -> Result<i32, LuaError>
Return the next byte from the stream as an i32, or [EOZ] at
end-of-stream.
This is the hot-path inline method corresponding to the zgetc macro.
When bytes remain in the current chunk no allocation occurs.
C’s macro uses (z)->n-- > 0, which reads n, tests it, then
decrements; when n == 0 the test is false (0 > 0) so fill is called
without decrementing. This preserves that: if self.n > 0 followed by
an explicit self.n -= 1.