Module io stdlib

io
Version:
0.3.3
License:
MIT
Dependencies from vmod:
0
Imports:
0
Imported by:
12
Repository:
OS-specific
Show selected OS-specific symbols.
Backend-specific
Show selected Backend-specific symbols.

Dependencies defined in v.mod

This section is empty.

Imports

This section is empty.

Overview

io provides common interfaces for buffered reading/writing of data.

Aliases

This section is empty.

Constants

This section is empty.

Sum types

This section is empty.

Functions

#fn cp

fn cp(mut src &Reader, mut dst &Writer) !

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.

#fn make_readerwriter

fn make_readerwriter(r Reader, w Writer) ReaderWriterImpl

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

#fn 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).

#fn 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).

Structs

#struct BufferedReader

pub 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
}

BufferedReader provides a buffered interface for a reader.

#fn new_buffered_reader

fn new_buffered_reader(o BufferedReaderConfig) &BufferedReader

new_buffered_reader creates a new BufferedReader.

#fn (&BufferedReader) read

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

read fufills the Reader interface.

#fn (&BufferedReader) free

fn (mut r &BufferedReader) free()

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

#fn (&BufferedReader) fill_buffer

fn (mut r &BufferedReader) fill_buffer() bool

fill_buffer attempts to refill the internal buffer and returns whether it got any data.

#fn (BufferedReader) needs_fill

fn (r BufferedReader) needs_fill() bool

needs_fill returns whether the buffer needs refilling.

#fn (BufferedReader) end_of_stream

fn (r BufferedReader) end_of_stream() bool

end_of_stream returns whether the end of the stream was reached.

#fn (&BufferedReader) read_line

fn (mut r &BufferedReader) read_line() !string

read_line attempts to read a line from the buffered reader it will read until it finds a new line character (\n) or the end of stream.

#struct BufferedReaderConfig

pub struct BufferedReaderConfig {
	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.

#struct MultiWriter

pub struct MultiWriter {
pub mut:
	writers []Writer
}

MultiWriter writes to all its writers.

#fn 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.

#fn (&MultiWriter) 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.

#struct Eof

pub struct Eof {
	Error
}

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

#struct NotExpected

pub struct NotExpected {
	cause string
	code  int
}

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

#fn (NotExpected) msg

fn (err NotExpected) msg() string

#fn (NotExpected) code

fn (err NotExpected) code() int

#struct ReadAllConfig

pub struct ReadAllConfig {
	read_to_end_of_stream bool
mut:
	reader Reader
}

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

Interfaces

#interface Reader

pub 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.

#interface RandomReader

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

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

#interface ReaderWriter

pub interface ReaderWriter {
	Reader
	Writer
}

ReaderWriter represents a stream that can be read and written.

#interface Writer

pub 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.

#interface RandomWriter

pub 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.

Enums

This section is empty.