mmap_read
result = mmap_read(m, start, len)
Input: m, start, len · Output: result
What it is
Reads a byte range out of a mapping without moving any cursor. m (mmap handle, from mmap_open) is the mapping to read from. start (number, byte offset from the beginning of the file) and len (number, byte count) select the range.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = mmap_read(m, start, len) | m —m (mmap handle, from mmap_open) is the mapping to read from.start numberstart (number, byte offset from the beginning of the file) and len (number, byte count) select the range.len numberstart (number, byte offset from the beginning of the file) and len (number, byte count) select the range. | result Vec or number | Returns a Vec of len numbers, each 0-255 — the same representation read_bin uses. A range that runs past the end of the file is a clear error, not a silent truncation (unlike read_bin's "graceful partial read" — an mmap handle always knows its own length up front via mmap_len, so a past-EOF range is far more likely a caller bug than an intentional "give me whatever's left"). |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
# The file is written here first. Mapping one the repository does not
# carry would make this block fail, and a block that fails is dropped
# from the page while the build still reports success.
write_array("big_dataset.bin", 0 to 511, type = "u8")
m = mmap_open("big_dataset.bin")
n = mmap_len(m)
header = mmap_read(m, 0, 16) # first 16 bytes, wherever they live
record5 = mmap_read(m, 5 * 64, 64) # the 6th fixed-width 64-byte record
print(n)
print(header)
print(record5)
output
512 [0, 1, 2, 3, 4, 5, 6, 7, ... (16 elements)] [255, 255, 255, 255, 255, 255, 255, 255, ... (64 elements)]
from file-io
f = fopen("mmap_demo.bin", "wb")
write_byte(f, 65)
write_byte(f, 66)
write_byte(f, 67)
write_byte(f, 68)
write_byte(f, 69)
write_byte(f, 70)
write_byte(f, 71)
write_byte(f, 72)
close(f)
m = mmap_open("mmap_demo.bin")
n = mmap_len(m)
first4 = mmap_read(m, 0, 4)
last4 = mmap_read(m, 4, 4)
print("n = {n}")
print("first4 = {first4}")
print("last4 = {last4}")
from tour