Skip to content
CryoCryo home

cursor

import std::io::cursor; · source

Types

Cursor

type struct Cursor {
    buffer: Array<u8>;
    pos:    u64;

    static new(buffer: Array<u8>) -> Cursor;
    static empty() -> Cursor;
    position(&this) -> u64;
    set_position(mut &this, pos: u64) -> void;
    len(&this) -> u64;
    as_bytes(&this) -> Slice<u8>;
    into_inner(mut &this) -> Array<u8>;
}

An in-memory byte buffer that is Read, Write, and Seek. This is how you run code written against the I/O traits against memory instead of a file or socket — the standard way to unit-test a reader or writer, or to build up a payload before handing it to a real sink.

import std::io::cursor;

mut c: Cursor = Cursor::empty();
c.write(header);
c.write(body);
mut bytes: Array<u8> = c.into_inner();

new(buffer) takes ownership of an existing buffer with the cursor at 0; empty() starts fresh. into_inner() moves the buffer out and leaves the cursor holding a fresh empty one.

Reads copy forward and return 0 at the end. Writes overwrite the bytes under the cursor and extend past the end; a write starting beyond the current end zero-fills the gap first, matching the sparse region a File seek-past-end leaves. Reads never error, and writes fail only on allocation failure, surfaced as WriteZero.

Properties

PropertyType
bufferArray<u8>
posu64

Constructors

static new(buffer: Array<u8>) -> Cursor;

Wrap an existing byte buffer with the cursor at position 0. Takes ownership of buffer; reads start at its first byte.

static empty() -> Cursor;

A cursor over a fresh empty buffer - the usual starting point for accumulating bytes with write.

Methods

position(&this) -> u64;

Current cursor position: the absolute byte offset from the start where the next read or write lands. Independent of the buffer length; may exceed it after a seek-past-end.

set_position(mut &this, pos: u64) -> void;

Set the cursor position directly. Positions past the buffer end are allowed and behave as documented on the type.

len(&this) -> u64;

Number of bytes currently in the buffer, independent of the cursor position.

as_bytes(&this) -> Slice<u8>;

Borrow the buffered bytes without moving the cursor. The slice is invalidated by any subsequent write (which may reallocate).

into_inner(mut &this) -> Array<u8>;

Move the buffer out, leaving the cursor holding a fresh empty buffer. The typical "collect the written bytes" finisher; the cursor stays valid and must still be dropped.

Trait implementations

implement trait Read for struct Cursor
implement trait Write for struct Cursor
implement trait Seek for struct Cursor
implement trait Drop for struct Cursor