get(index: int) - > Option<int>- Module
- std.
bytes - Receiver
- Bytes
- Minimum arity
- 1
- Maximum arity
- 1
Declaration and parameters
LinkAvailability
Included by default: Pending
Import std.bytes in language mode 5.21.
The current compiler selects std/bytes.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 956 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 52 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 956 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 407 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296 - compiler/crates/topaz_value/src/value/members.rs at
b48527d0a174· 250
Receiver
Bytes
Parameter constraints
A Bytes value is an immutable ordered sequence of octets. Each octet is an int from 0 through 255.
Bytes.empty returns the empty sequence. Bytes.fromArray accepts an Array<int> and returns an independent snapshot in Ok.
Bytes.fromArray returns Err with a string identifying the first element outside 0 through 255. It does not wrap, clamp, or return a partial sequence.
Bytes.toArray returns a new Array<int> in byte order. Mutating that array or the original input array does not change an existing Bytes value.
Bytes.concat returns the receiver bytes followed by the argument bytes. Either input may be empty and both inputs retain their contents.
Bytes.get uses a zero-based byte index. It returns Some(byte) within the sequence and None() for every negative or past-end index.
Bytes.length is the number of bytes and is read as a property. Compare it with zero to test emptiness.
Receivers and supplied arguments evaluate once from left to right before the operation begins. These operations perform no I/O or text normalization.
A correctly typed lookup or concatenation has no data-dependent Result failure. Allocation exhaustion is an operational failure and is not a decoded data error.
Related rule: Construct and inspect immutable byte sequencesResult
The declared result type is Option<int>.
Effects
Failures
Result and effects
LinkRead a zero-based byte index as Some(byte). Negative and past-end indices return None().
Resolved contracts
LinkRelated entries
LinkConstruct and inspect immutable byte sequences
LinkConstruct and inspect immutable byte sequences
A Bytes value is an immutable ordered sequence of octets. Each octet is an int from 0 through 255.
Bytes.empty returns the empty sequence. Bytes.fromArray accepts an Array<int> and returns an independent snapshot in Ok.
Bytes.fromArray returns Err with a string identifying the first element outside 0 through 255. It does not wrap, clamp, or return a partial sequence.
Bytes.toArray returns a new Array<int> in byte order. Mutating that array or the original input array does not change an existing Bytes value.
Bytes.concat returns the receiver bytes followed by the argument bytes. Either input may be empty and both inputs retain their contents.
Bytes.get uses a zero-based byte index. It returns Some(byte) within the sequence and None() for every negative or past-end index.
Bytes.length is the number of bytes and is read as a property. Compare it with zero to test emptiness.
Receivers and supplied arguments evaluate once from left to right before the operation begins. These operations perform no I/O or text normalization.
A correctly typed lookup or concatenation has no data-dependent Result failure. Allocation exhaustion is an operational failure and is not a decoded data error.
Why
Exact octets keep binary payloads independent of text encoding. Snapshot conversion prevents later array edits from silently changing a payload.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 67 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 73 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 294 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 342 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 351 - compiler/crates/topaz_product_runtime/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bytes-valuesatworking-tree
Preserve empty values and byte order
Linkimport std.bytes { Bytes }import std.ioimport std.testlet empty = test.assertOk(Bytes.fromArray([]))let value = test.assertOk(Bytes.fromArray([0, 128, 255]))io.writeLine(Bytes.empty().toHex())io.writeLine(empty.concat(value).toHex())io.writeLine(value.concat(test.assertOk(Bytes.fromArray([1]))).toHex())
0080ff 0080ff01
Expected result
Expected result
Execution completes. Exit code 0.
0080ff0080ff01
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 67 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 73 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 294 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 342 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 351 - compiler/crates/topaz_product_runtime/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bytes-valuesatworking-tree
Reject out-of-range array elements
Linkimport std.bytes { Bytes }import std.iofor values in ([[0, -1, 255], [0, 256, 255]]) {match (Bytes.fromArray(values)) {case Err(_) => io.writeLine("invalid byte")case Ok(_) => io.writeLine("accepted")}}
invalid byte invalid byte
Expected result
Expected result
Execution completes. Exit code 0.
invalid byteinvalid byte
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 67 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 73 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 294 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 342 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 351 - compiler/crates/topaz_product_runtime/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bytes-valuesatworking-tree
Read optional bytes at both boundaries
Linkimport std.bytes { Bytes }import std.ioimport std.testlet value = test.assertOk(Bytes.fromArray([0, 128, 255]))for index in ([-1, 0, 2, 3]) {match (value.get(index)) {case Some(byte) => io.writeLine("{byte}")case None() => io.writeLine("absent")}}
absent 0 255 absent
Expected result
Expected result
Execution completes. Exit code 0.
absent0255absent
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 67 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 73 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 294 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 342 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 351 - compiler/crates/topaz_product_runtime/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bytes-valuesatworking-tree
Keep array edits independent from bytes
Linkimport std.bytes { Bytes }import std.ioimport std.testlet mut input = [0, 255]let value = test.assertOk(Bytes.fromArray(input))let mut output = value.toArray()input[0] = 7output[1] = 8io.writeLine(value.toHex())io.writeLine("{input[0]},{output[1]}")
00ff 7,8
Expected result
Expected result
Execution completes. Exit code 0.
00ff7,8
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 67 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 73 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 294 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 342 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 351 - compiler/crates/topaz_product_runtime/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bytes-valuesatworking-tree
Evaluate the receiver before its argument
Linkimport std.bytes { Bytes }import std.iofunction receiver() -> Bytes {io.writeLine("receiver")return "a".encodeUtf8()}function argument() -> Bytes {io.writeLine("argument")return "b".encodeUtf8()}io.writeLine(receiver().concat(argument()).toHex())
receiver argument 6162
Expected result
Expected result
Execution completes. Exit code 0.
receiverargument6162
Source evidence
- compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 67 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 73 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 294 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 342 - compiler/crates/topaz_value/src/value/builtins/bytes.rs at
912a5e86e1f9· 351 - compiler/crates/topaz_product_runtime/src/lib.rs at
912a5e86e1f9· 17 -
topaz.stdlib.contract.bytes-valuesatworking-tree