Skip to content
CryoCryo home

cursor

import std::io::cursor; · source

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.

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