Files
herolib/aiprompts/v_core/io/io.md
2025-09-02 07:28:13 +02:00

9.1 KiB

module io

Contents

Constants

const read_all_len = 10 * 1024

[Return to contents]

const read_all_grow_len = 1024

[Return to contents]

cp

fn cp(mut src Reader, mut dst Writer, params CopySettings) !

cp copies from src to dst by allocating a maximum of 1024 bytes buffer for reading until either EOF is reached on src or an error occurs. An error is returned if an error is encountered during write.

[Return to contents]

make_readerwriter

fn make_readerwriter(r Reader, w Writer) ReaderWriterImpl

make_readerwriter takes a rstream and a wstream and makes an rwstream with them.

[Return to contents]

new_buffered_reader

fn new_buffered_reader(o BufferedReaderConfig) &BufferedReader

new_buffered_reader creates a new BufferedReader.

[Return to contents]

new_buffered_writer

fn new_buffered_writer(o BufferedWriterConfig) !&BufferedWriter

new_buffered_writer creates a new BufferedWriter with the specified BufferedWriterConfig. Returns an error when cap is 0 or negative.

[Return to contents]

new_multi_writer

fn new_multi_writer(writers ...Writer) Writer

new_multi_writer returns a Writer that writes to all writers. The write function of the returned Writer writes to all writers of the MultiWriter, returns the length of bytes written, and if any writer fails to write the full length an error is returned and writing to other writers stops, and if any writer returns an error the error is returned immediately and writing to other writers stops.

[Return to contents]

read_all

fn read_all(config ReadAllConfig) ![]u8

read_all reads all bytes from a reader until either a 0 length read or if read_to_end_of_stream is true then the end of the stream (none).

[Return to contents]

read_any

fn read_any(mut r Reader) ![]u8

read_any reads any available bytes from a reader (until the reader returns a read of 0 length).

[Return to contents]

RandomReader

interface RandomReader {
	read_from(pos u64, mut buf []u8) !int
}

RandomReader represents a stream of readable data from at a random location.

[Return to contents]

RandomWriter

interface RandomWriter {
	write_to(pos u64, buf []u8) !int
}

RandomWriter is the interface that wraps the write_to method, which writes buf.len bytes to the underlying data stream at a random pos.

[Return to contents]

Reader

interface Reader {
	// read reads up to buf.len bytes and places
	// them into buf.
	// A type that implements this should return
	// `io.Eof` on end of stream (EOF) instead of just returning 0
mut:
	read(mut buf []u8) !int
}

Reader represents a stream of data that can be read.

[Return to contents]

ReaderWriter

interface ReaderWriter {
	Reader
	Writer
}

ReaderWriter represents a stream that can be read and written.

[Return to contents]

Writer

interface Writer {
mut:
	write(buf []u8) !int
}

Writer is the interface that wraps the write method, which writes buf.len bytes to the underlying data stream.

[Return to contents]

ReaderWriterImpl

read

fn (mut r ReaderWriterImpl) read(mut buf []u8) !int

read reads up to buf.len bytes into buf. It returns the number of bytes read or any error encountered.

[Return to contents]

write

fn (mut r ReaderWriterImpl) write(buf []u8) !int

write writes buf.len bytes from buf to the underlying data stream. It returns the number of bytes written or any error encountered.

[Return to contents]

BufferedReadLineConfig

struct BufferedReadLineConfig {
pub:
	delim u8 = `\n` // line delimiter
}

BufferedReadLineConfig are options that can be given to the read_line() function.

[Return to contents]

BufferedReader

struct BufferedReader {
mut:
	reader Reader
	buf    []u8
	offset int // current offset in the buffer
	len    int
	fails  int // how many times fill_buffer has read 0 bytes in a row
	mfails int // maximum fails, after which we can assume that the stream has ended
pub mut:
	end_of_stream bool // whether we reached the end of the upstream reader
	total_read    int  // total number of bytes read
}

BufferedReader provides a buffered interface for a reader.

[Return to contents]

read

fn (mut r BufferedReader) read(mut buf []u8) !int

read fufills the Reader interface.

[Return to contents]

free

fn (mut r BufferedReader) free()

free deallocates the memory for a buffered reader's internal buffer.

[Return to contents]

end_of_stream

fn (r BufferedReader) end_of_stream() bool

end_of_stream returns whether the end of the stream was reached.

[Return to contents]

read_line

fn (mut r BufferedReader) read_line(config BufferedReadLineConfig) !string

read_line attempts to read a line from the buffered reader. It will read until it finds the specified line delimiter such as (\n, the default or \0) or the end of stream.

[Return to contents]

BufferedReaderConfig

struct BufferedReaderConfig {
pub:
	reader  Reader
	cap     int = 128 * 1024 // large for fast reading of big(ish) files
	retries int = 2          // how many times to retry before assuming the stream ended
}

BufferedReaderConfig are options that can be given to a buffered reader.

[Return to contents]

BufferedWriter

struct BufferedWriter {
mut:
	n  int
	wr Writer
pub mut:
	buf []u8
}

[Return to contents]

reset

fn (mut b BufferedWriter) reset()

reset resets the buffer to its initial state.

[Return to contents]

buffered

fn (b BufferedWriter) buffered() int

buffered returns the number of bytes currently stored in the buffer.

[Return to contents]

flush

fn (mut b BufferedWriter) flush() !

flush writes the buffered data to the underlying writer and clears the buffer, ensures all data is written. Returns an error if the writer fails to write all buffered data.

[Return to contents]

available

fn (b BufferedWriter) available() int

available returns the amount of available space left in the buffer.

[Return to contents]

write

fn (mut b BufferedWriter) write(src []u8) !int

write writes src in the buffer, flushing it to the underlying writer as needed, and returns the number of bytes written.

[Return to contents]

BufferedWriterConfig

struct BufferedWriterConfig {
pub:
	writer Writer
	cap    int = 128 * 1024
}

[Return to contents]

CopySettings

struct CopySettings {
pub mut:
	buffer_size int = 64 * 1024 // The buffer size used during the copying. A larger buffer is more performant, but uses more RAM.
}

CopySettings provides additional options to io.cp

[Return to contents]

Eof

struct Eof {
	Error
}

Eof error means that we reach the end of the stream.

[Return to contents]

MultiWriter

struct MultiWriter {
pub mut:
	writers []Writer
}

MultiWriter writes to all its writers.

[Return to contents]

write

fn (mut m MultiWriter) write(buf []u8) !int

write writes to all writers of the MultiWriter. Returns the length of bytes written. If any writer fails to write the full length an error is returned and writing to other writers stops. If any writer returns an error the error is returned immediately and writing to other writers stops.

[Return to contents]

NotExpected

struct NotExpected {
	cause string
	code  int
}

NotExpected is a generic error that means that we receave a not expected error.

[Return to contents]

ReadAllConfig

struct ReadAllConfig {
pub:
	read_to_end_of_stream bool
	reader                Reader
}

ReadAllConfig allows options to be passed for the behaviour of read_all.

[Return to contents]

Powered by vdoc. Generated on: 2 Sep 2025 07:19:15