from Array(values: Array<int>) - > Result<Bytes, string>- Модуль
- std.
bytes → - Получатель
- Неприменимо
- Минимальное число аргументов
- 1
- Максимальное число аргументов
- 1
Declaration and parameters
СсылкаДоступность
Включено по умолчанию · Не определено
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.
Получатель
Ограничения параметров
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.
Связанное правило · Construct and inspect immutable byte sequences →Результат
The declared result type is Result<Bytes, string>.
Эффекты
Ошибки
Result and effects
СсылкаSnapshot integer values from 0 through 255 into Ok with Bytes. An out-of-range element returns Err with its first invalid index.
Resolved contracts
СсылкаRelated entries
СсылкаConstruct and inspect immutable byte sequences
СсылкаConstruct 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.
Причина
Exact octets keep binary payloads independent of text encoding. Snapshot conversion prevents later array edits from silently changing a payload.
Preserve empty values and byte order
Ссылкаimport 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
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
0080ff0080ff01
Reject out-of-range array elements
Ссылкаimport 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
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
invalid byteinvalid byte
Read optional bytes at both boundaries
Ссылкаimport 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
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
absent0255absent
Keep array edits independent from bytes
Ссылкаimport 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
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
00ff7,8
Evaluate the receiver before its argument
Ссылкаimport 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
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
receiverargument6162