std.fs.file.ReadWriteFile
type pub inline ReadWriteFileA file that can be used for both reads and writes.
Static methods
append
Show source codeHide source code
fn pub static append(path: ref Path) -> Result[ReadWriteFile, Error] {
match
sys.open_file(
path.to_string,
read: true,
write: true,
append: true,
truncate: false,
)
{
case Ok(fd) -> Result.Ok(ReadWriteFile(fd))
case Error(e) -> Result.Error(e)
}
}fn pub static append(path: ref Path) -> Result[ReadWriteFile, Error]Opens a file for both reading and appending:
Examples
import std.fs.file (ReadWriteFile)
ReadWriteFile.append('/dev/null'.to_path)
new
Show source codeHide source code
fn pub static new(path: ref Path) -> Result[ReadWriteFile, Error] {
match
sys.open_file(
path.to_string,
read: true,
write: true,
append: false,
truncate: false,
)
{
case Ok(fd) -> Result.Ok(ReadWriteFile(fd))
case Error(e) -> Result.Error(e)
}
}fn pub static new(path: ref Path) -> Result[ReadWriteFile, Error]Opens a file for both reading and writing:
Examples
import std.fs.file (ReadWriteFile)
ReadWriteFile.new('/dev/null'.to_path)
Instance methods
flush
Show source codeHide source code
fn pub mut flush -> Result[Nil, Error] {
sys.flush_file(@fd)
}fn pub mut flush -> Result[Nil, Error]Flushes any pending writes to the file system.
Flushing writes is a potentially expensive operation, and unnecessarily calling this method may degrade performance.
When flushing data to disk it's important to remember that the actual behaviour may vary based on the type of file system, operating system and storage hardware that's used. In particular, it's possible for one of these components to say "Yup, I totally flushed the data, you're all good!" when in fact they have not fully flushed the data.
metadata
Show source codeHide source code
fn pub metadata -> Result[Metadata, Error] {
sys.file_metadata(@fd)
}fn pub metadata -> Result[Metadata, Error]Returns a metadata about the current file, such as its size and creation time.
Errors
This method returns an Error if the underlying system call fails, such as
when the file no longer exists.
Examples
import std.fs.file (ReadWriteFile)
ReadWriteFile
.new('/test.txt')
.or_panic_with('failed to open the file')
.metadata
.or_panic_with('failed to get the metadata')
.size # => 1234
Show source codeHide source code
fn pub mut print[B: Bytes](bytes: ref B) -> Result[Nil, E] {
try write(bytes)
write('\n')
}fn pub mut print[B: Bytes](bytes: ref B) -> Result[Nil, E]Writes the entirety of bytes to the underlying stream, followed by
a Unix newline.
read
Show source codeHide source code
fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, Error] {
sys.read_file(@fd, into, size)
}fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, Error]Reads up to size bytes from self into the given ByteArray, returning
the number of bytes read.
The into argument is the ByteArray to read the bytes into. The capacity
of this ByteArray is increased automatically if necessary.
The size argument specifies how many bytes are to be read.
The return value is the number of bytes read.
The number of bytes read may be less than size. This can happen for
different reasons, such as when all input is consumed or not enough data is
available (yet).
read_all
Show source codeHide source code
fn pub mut read_all(bytes: mut ByteArray) -> Result[Int, E] {
let mut total = 0
let mut read_size = INITIAL_READ_ALL_SIZE
loop {
match read(into: bytes, size: read_size) {
case Ok(0) -> return Result.Ok(total)
case Ok(n) -> {
total += n
# To reduce the number of calls to `Reader.read` when there's lots of
# input to consume, we increase the read size if deemed beneficial.
if read_size < MAX_READ_ALL_SIZE and n == read_size { read_size *= 2 }
}
case Error(e) -> throw e
}
}
}fn pub mut read_all(bytes: mut ByteArray) -> Result[Int, E]Reads from self into the given ByteArray, returning when all input is
consumed.
The return value is the number of bytes read.
Errors
This method returns an Error if the underlying call to Read.read returns
an Error.
read_exact
Show source codeHide source code
fn pub mut read_exact(
into: mut ByteArray,
size: Int,
) -> Result[Nil, ReadExactError[E]] {
let mut pending = size
while pending > 0 {
match read(into, pending) {
case Ok(0) if pending > 0 -> throw ReadExactError.EndOfInput
case Ok(n) -> pending -= n
case Error(e) -> throw ReadExactError.Read(e)
}
}
Result.Ok(nil)
}fn pub mut read_exact(into: mut ByteArray, size: Int) -> Result[Nil, ReadExactError[E]]Reads exactly size bytes into into.
Whereas Read.read might return early if fewer bytes are available in the
input stream, Read.read_exact continues reading until the desired amount
of bytes is read.
Errors
If the end of the input stream is encountered before filling the buffer, an
Error.EndOfInput error is returned.
If an error is returned, no assumption can be made about the state of the
into buffer, i.e. there's no guarantee data read so far is in the buffer
in the event of an error.
seek
Show source codeHide source code
fn pub mut seek(position: SeekFrom) -> Result[Int, Error] {
sys.seek_to(@fd, position)
}fn pub mut seek(position: SeekFrom) -> Result[Int, Error]Seeks to the given offset, returning the new offset.
Upon success the new offset (in bytes) is returned.
Seeking beyond the end of the stream is allowed, but seeking before the start of the stream is an error.
write
Show source codeHide source code
fn pub mut write[B: Bytes](bytes: ref B) -> Result[Nil, Error] {
write_all_internal(bytes.pointer, bytes.size)
}fn pub mut write[B: Bytes](bytes: ref B) -> Result[Nil, Error]Writes the entirety of bytes to the underlying stream.
Types implementing this method must guarantee that upon returning from this
method, either all of the data is written and a Ok(Nil) is returned, or
an Error is returned.
Implemented traits
Drop
impl Drop for ReadWriteFileRead
impl Read[Error] for ReadWriteFileSeek
impl Seek[Error] for ReadWriteFileWrite
impl Write[Error] for ReadWriteFile