std.fs.file.WriteOnlyFile
type pub inline WriteOnlyFileA file that can only be used for writes.
Static methods
append
Show source codeHide source code
fn pub static append(path: Path) -> Result[WriteOnlyFile, Error] {
match
sys.open_file(
path.to_string,
read: false,
write: true,
append: true,
truncate: false,
)
{
case Ok(fd) -> Result.Ok(WriteOnlyFile(fd))
case Error(e) -> Result.Error(e)
}
}fn pub static append(path: Path) -> Result[WriteOnlyFile, Error]Opens a file in append-only mode.
Examples
import std.fs.file (WriteOnlyFile)
WriteOnlyFile.append('/dev/null'.to_path)
new
Show source codeHide source code
fn pub static new(path: ref Path) -> Result[WriteOnlyFile, Error] {
match
sys.open_file(
path.to_string,
read: false,
write: true,
append: false,
truncate: true,
)
{
case Ok(fd) -> Result.Ok(WriteOnlyFile(fd))
case Error(e) -> Result.Error(e)
}
}fn pub static new(path: ref Path) -> Result[WriteOnlyFile, Error]Opens a file in write-only mode.
Examples
import std.fs.file (WriteOnlyFile)
WriteOnlyFile.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 (WriteOnlyFile)
WriteOnlyFile
.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.
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 WriteOnlyFileSeek
impl Seek[Error] for WriteOnlyFileWrite
impl Write[Error] for WriteOnlyFile