IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

FileDescriptor

struct FileDescriptor

Owning file descriptor that is closed explicitly with close().

Movable but not copyable, so exactly one value owns the descriptor. -1 is the niche, so Optional[FileDescriptor] is the size of a descriptor.

Examples:

from std.io import FileDescriptor

def release(var fd: FileDescriptor) raises:
fd^.close()

Implemented traits​

AnyType, Movable, UnsafeNicheable, _FieldNicheable

Methods​

__init__​

def __init__(out self, *, unsafe_raw_fd: RawFileDescriptor)

Takes ownership of a raw file descriptor.

Args:

  • ​unsafe_raw_fd (RawFileDescriptor): The descriptor to own. Nothing else may close it, so never pass stdin, stdout, stderr or the descriptor behind a FileHandle.

def __init__(out self, *, copy_borrowed: BorrowedFileDescriptor)

Constructs a new owner by duplicating a borrowed descriptor.

The new FileDescriptor owns a fresh descriptor that refers to the same open file description as copy_borrowed, so the two share the file offset and status flags but are closed independently. The copy is close-on-exec, so child processes don't inherit it.

Args:

Raises:

If the duplication fails.

close​

def close(deinit self)

Closes the owned file descriptor.

Raises:

If the close fails. A failed close can mean buffered writes never reached the file.