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).
BorrowedFileDescriptor
struct BorrowedFileDescriptor[origin: ImmOrigin]
Copyable, non-owning view of a FileDescriptor.
The view carries the owner's origin, so it cannot outlive the owner, and
it never closes the descriptor. -1 is the niche, so
Optional[BorrowedFileDescriptor] is the size of a descriptor.
Examples:
from std.io import BorrowedFileDescriptor, FileDescriptor
def share(var fd: FileDescriptor) raises:
var first = BorrowedFileDescriptor(fd)
var second = first
fd^.close()
Parameters
- origin (
ImmOrigin): The origin of theFileDescriptorthis view refers to.
Implemented traits
AnyType,
Copyable,
Deinitable,
ImplicitlyCopyable,
Movable,
RegisterPassable,
TrivialRegisterPassable,
UnsafeNicheable,
Writer,
_FieldNicheable
Methods
__init__
@implicit
def __init__(ref[origin] fd: FileDescriptor) -> Self
Constructs a view of a FileDescriptor.
Args:
- fd (
FileDescriptor): The owning descriptor to view.
@implicit
def __init__(ref[origin] f: FileHandle) -> Self
Constructs a view of the descriptor behind a FileHandle.
Args:
- f (
FileHandle): The file handle whose descriptor to view.
isatty
def isatty(self) -> Bool
Checks whether the descriptor refers to a terminal.
Examples:
if stdout.isatty():
print("Running in a terminal")
Returns:
Bool: True if the descriptor is open and connected to a terminal,
otherwise False. Always False on GPUs.
write_bytes
def write_bytes(mut self, bytes: ImmSpan[UInt8])
Writes a span of bytes to the descriptor.
Writing through a view is sound because write needs no exclusive
access to the descriptor.
Args:
- bytes (
ImmSpan[UInt8]): The bytes to write.
write_string
def write_string(mut self, string: StringSpan)
Writes a StringSpan to the descriptor.
Examples:
var out = stdout
out.write("Hello, ", 42)
Args:
- string (
StringSpan): The string to write.