set(index: int, value: int) - > ()- Module
- std.
bytes - Receiver
- Byte
Buffer - Minimum arity
- 2
- Maximum arity
- 2
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· 985 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 52 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 985 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 417 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296 - compiler/crates/topaz_value/src/value/members.rs at
b48527d0a174· 257
Receiver
ByteBuffer
Parameter constraints
ByteBuffer.get and ByteBuffer.set use zero-based byte indices from zero through length minus one. Get returns an int byte.
ByteBuffer.set replaces one byte. ByteBuffer.fill replaces length bytes beginning at start. Both require values from 0 through 255.
ByteBuffer.copy receives the source buffer, sourceStart, targetStart, and length in that order. The receiver is the destination.
Fill and copy use byte intervals from each start to start plus length. Starts and lengths must be nonnegative. Every complete source and destination interval must fit its buffer without arithmetic overflow.
A zero length region at the end of a buffer is valid. A start beyond the end is invalid even when length is zero.
Copy observes the source interval before its first destination write. Copying within one buffer therefore supports overlap in either direction.
Set, fill, and copy return unit. They require a let mut root binding and update its destination value under collection value semantics.
The receiver and supplied arguments evaluate once from left to right. Every range and byte check finishes before the first write.
An invalid index or region produces TPZ4001 at runtime. An invalid byte value produces TPZ5001 at runtime, including a fill of zero bytes.
A rejected operation performs no buffer writes. Effects performed while evaluating its receiver or arguments remain visible.
When several preconditions fail, the selected failing precondition is unspecified. The operation still completes every argument evaluation before validation and performs no writes.
Related rule: Validate buffer regions before writingResult
The declared result type is ().
Effects
Failures
Result and effects
LinkReplace one in-range byte after validating its index and value. Return unit without resizing the buffer.
Resolved contracts
LinkRelated entries
LinkValidate buffer regions before writing
LinkValidate buffer regions before writing
ByteBuffer.get and ByteBuffer.set use zero-based byte indices from zero through length minus one. Get returns an int byte.
ByteBuffer.set replaces one byte. ByteBuffer.fill replaces length bytes beginning at start. Both require values from 0 through 255.
ByteBuffer.copy receives the source buffer, sourceStart, targetStart, and length in that order. The receiver is the destination.
Fill and copy use byte intervals from each start to start plus length. Starts and lengths must be nonnegative. Every complete source and destination interval must fit its buffer without arithmetic overflow.
A zero length region at the end of a buffer is valid. A start beyond the end is invalid even when length is zero.
Copy observes the source interval before its first destination write. Copying within one buffer therefore supports overlap in either direction.
Set, fill, and copy return unit. They require a let mut root binding and update its destination value under collection value semantics.
The receiver and supplied arguments evaluate once from left to right. Every range and byte check finishes before the first write.
An invalid index or region produces TPZ4001 at runtime. An invalid byte value produces TPZ5001 at runtime, including a fill of zero bytes.
A rejected operation performs no buffer writes. Effects performed while evaluating its receiver or arguments remain visible.
When several preconditions fail, the selected failing precondition is unspecified. The operation still completes every argument evaluation before validation and performs no writes.
Why
Checking complete regions prevents a malformed count from leaving half-written data. Reading the source interval before writing gives one result for every overlap direction.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 89 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 156 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 195 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 232 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 283 -
topaz.stdlib.contract.byte-buffer-mutationatworking-tree - topaz.decision.collection-value-semantics.1 at
topaz.decisi
Copy overlapping regions and preserve an earlier buffer value
Linkimport std.bytes { Bytes, ByteBuffer }import std.ioimport std.testlet mut buffer = ByteBuffer.fromBytes(test.assertOk(Bytes.fromHex("000102030405")))let alias = bufferbuffer.copy(buffer, 0, 1, 5)io.writeLine(buffer.toBytes().toHex())buffer.fill(2, 2, 255)buffer.set(0, 128)io.writeLine(buffer.toBytes().toHex())buffer.copy(buffer, 1, 0, 5)buffer.fill(6, 0, 0)buffer.copy(buffer, 6, 6, 0)io.writeLine(buffer.toBytes().toHex())io.writeLine(alias.toBytes().toHex())io.writeLine("{alias.get(0)}")
000001020304 8000ffff0304 00ffff030404 000102030405 0
Expected result
Expected result
Execution completes. Exit code 0.
0000010203048000ffff030400ffff0304040001020304050
Source evidence
- compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 89 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 156 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 195 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 232 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 283 -
topaz.stdlib.contract.byte-buffer-mutationatworking-tree - topaz.decision.collection-value-semantics.1 at
topaz.decisi
Keep an independent source buffer unchanged
Linkimport std.bytes { ByteBuffer }import std.iolet source = ByteBuffer.fromBytes("abc".encodeUtf8())let mut target = ByteBuffer.allocate(4, 0)target.copy(source, 1, 1, 2)io.writeLine(target.toBytes().toHex())io.writeLine(source.toBytes().toHex())
00626300 616263
Expected result
Expected result
Execution completes. Exit code 0.
00626300616263
Source evidence
- compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 89 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 156 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 195 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 232 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 283 -
topaz.stdlib.contract.byte-buffer-mutationatworking-tree - topaz.decision.collection-value-semantics.1 at
topaz.decisi
Reject a read at the exclusive end
Linkimport std.bytes { ByteBuffer }let buffer = ByteBuffer.allocate(2)buffer.get(2)
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 89 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 156 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 195 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 232 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 283 -
topaz.stdlib.contract.byte-buffer-mutationatworking-tree - topaz.decision.collection-value-semantics.1 at
topaz.decisi
Reject a negative write index
Linkimport std.bytes { ByteBuffer }let mut buffer = ByteBuffer.allocate(2)buffer.set(-1, 0)
No stdout.
Expected result
Expected result
Rejection during run. Nonzero exit.
TPZ4001byte-buffer-negative-bound atmain.tpz:3:1
Source evidence
- compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 89 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 156 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 195 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 232 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 283 -
topaz.stdlib.contract.byte-buffer-mutationatworking-tree - topaz.decision.collection-value-semantics.1 at
topaz.decisi
Reject a fill whose complete region does not fit
Linkimport std.bytes { ByteBuffer }let mut buffer = ByteBuffer.allocate(2)buffer.fill(1, 2, 0)
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 89 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 156 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 195 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 232 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 283 -
topaz.stdlib.contract.byte-buffer-mutationatworking-tree - topaz.decision.collection-value-semantics.1 at
topaz.decisi
Reject an empty copy beyond the buffer end
Linkimport std.bytes { ByteBuffer }let mut buffer = ByteBuffer.allocate(2)buffer.copy(buffer, 3, 2, 0)
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 89 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 156 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 195 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 232 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 283 -
topaz.stdlib.contract.byte-buffer-mutationatworking-tree - topaz.decision.collection-value-semantics.1 at
topaz.decisi
Reject an invalid byte at a valid write index
Linkimport std.bytes { ByteBuffer }import std.iolet mut buffer = ByteBuffer.allocate(1)buffer.set(0, -1)io.writeLine("after")
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 89 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 156 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 195 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 232 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 283 -
topaz.stdlib.contract.byte-buffer-mutationatworking-tree - topaz.decision.collection-value-semantics.1 at
topaz.decisi
Validate the byte for a fill of zero bytes
Linkimport std.bytes { ByteBuffer }import std.iolet mut buffer = ByteBuffer.allocate(1)buffer.fill(1, 0, 256)io.writeLine("after")
No stdout.
Expected result
Source evidence
- compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 89 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 156 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 195 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 232 - compiler/crates/topaz_value/src/value/builtins/byte_buffer.rs at
912a5e86e1f9· 283 -
topaz.stdlib.contract.byte-buffer-mutationatworking-tree - topaz.decision.collection-value-semantics.1 at
topaz.decisi