38 lines
1.6 KiB
Plaintext
38 lines
1.6 KiB
Plaintext
#! @file readable.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the readable trait, which anything bytes are read from implements: a
|
||
|
|
#! file, the console, a reader, or a view of text.
|
||
|
|
import system;
|
||
|
|
module std::io;
|
||
|
|
|
||
|
|
|
||
|
|
#! Anything bytes are read from.
|
||
|
|
#!
|
||
|
|
#! A reference that writes, `&mut` one, is readable where what it refers to is,
|
||
|
|
#! so a generic given `&mut f` reads `f` without owning it, and `&mut readable`
|
||
|
|
#! reads whatever it refers to. One that only reads is not, since reading moves
|
||
|
|
#! where the next read starts.
|
||
|
|
pub trait readable {
|
||
|
|
#! Reads what has arrived, waiting only where nothing has.
|
||
|
|
#! @param this The stream to read.
|
||
|
|
#! @param into Where the bytes read are written.
|
||
|
|
#! @returns How many bytes were read, which may be fewer than there is room
|
||
|
|
#! for, and is zero only at the end, or why nothing could be read.
|
||
|
|
def read(&mut this, into: slice<u8>): result<u64, io_error>;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reads bytes from the front of the view, which then views only what is left,
|
||
|
|
#! so a view of text is read as any stream is. A copy of the view reads the text
|
||
|
|
#! again.
|
||
|
|
#! @param this The view to read from, which shrinks by what is read.
|
||
|
|
#! @param into Where the bytes read are written.
|
||
|
|
#! @returns How many bytes were read, which is zero once the view is empty.
|
||
|
|
pub def strview::read(&mut this, into: slice<u8>): result<u64, io_error> {
|
||
|
|
val count := into.length < this.length ? into.length : this.length;
|
||
|
|
memcpy(into.data, this.data, count);
|
||
|
|
this = this.substr(count, this.length - count);
|
||
|
|
return count;
|
||
|
|
}
|