to Bytes() - > Bytes- Модуль
- std.
bytes → - Получатель
- Byte
Buffer - Минимальное число аргументов
- 0
- Максимальное число аргументов
- 0
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.
Получатель
ByteBuffer
Ограничения параметров
ByteBuffer.allocate accepts a nonnegative int length and an int initial value from 0 through 255. The initial value defaults to zero.
The new buffer has exactly that many copies of the initial byte. Zero length is valid and still requires a valid initial byte.
A negative length produces TPZ4001 at runtime. An invalid initial byte or an allocation that cannot be satisfied produces TPZ5001 at runtime.
ByteBuffer.fromBytes converts an immutable Bytes value into a mutable ByteBuffer value containing the same bytes.
ByteBuffer.toBytes converts the current buffer contents into an immutable Bytes value.
ByteBuffer.length is a read-only byte count property. Buffer operations never change its length.
Assignment, argument passing, return, capture, and aggregate storage follow collection value semantics.
Supplied expressions evaluate once from left to right before allocation or conversion. These operations have no I/O effects.
Связанное правило · Allocate buffers and convert byte values →Результат
The declared result type is Bytes.
Эффекты
Ошибки
Result and effects
СсылкаConvert the current buffer contents into an immutable Bytes value.
Resolved contracts
СсылкаRelated entries
СсылкаAllocate buffers and convert byte values
СсылкаAllocate buffers and convert byte values
ByteBuffer.allocate accepts a nonnegative int length and an int initial value from 0 through 255. The initial value defaults to zero.
The new buffer has exactly that many copies of the initial byte. Zero length is valid and still requires a valid initial byte.
A negative length produces TPZ4001 at runtime. An invalid initial byte or an allocation that cannot be satisfied produces TPZ5001 at runtime.
ByteBuffer.fromBytes converts an immutable Bytes value into a mutable ByteBuffer value containing the same bytes.
ByteBuffer.toBytes converts the current buffer contents into an immutable Bytes value.
ByteBuffer.length is a read-only byte count property. Buffer operations never change its length.
Assignment, argument passing, return, capture, and aggregate storage follow collection value semantics.
Supplied expressions evaluate once from left to right before allocation or conversion. These operations have no I/O effects.
Причина
A fixed length gives binary algorithms stable byte positions. Explicit conversion distinguishes mutable construction from immutable byte data.
Allocate zero-filled and initialized buffers
Ссылкаimport std.bytes { ByteBuffer }import std.ioio.writeLine(ByteBuffer.allocate(0).toBytes().toHex())io.writeLine(ByteBuffer.allocate(3).toBytes().toHex())io.writeLine(ByteBuffer.allocate(2, 255).toBytes().toHex())
000000
ffff
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
000000ffff
Keep the original buffer and converted bytes unchanged
Ссылкаimport std.bytes { ByteBuffer }import std.iolet source = "ab".encodeUtf8()let buffer = ByteBuffer.fromBytes(source)let independent = ByteBuffer.fromBytes(source)let before = buffer.toBytes()let mut alias = bufferalias.set(0, 99)io.writeLine(source.toHex())io.writeLine(before.toHex())io.writeLine(independent.toBytes().toHex())io.writeLine(buffer.toBytes().toHex())
6162
6162
6162
6162
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
6162616261626162
Reject a negative buffer length
Ссылкаimport std.bytes { ByteBuffer }ByteBuffer.allocate(-1)
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения run. Ненулевой код завершения.
byte-buffer-negative-bound at
main.tpz:2:1
Validate the initial byte of an empty buffer
Ссылкаimport std.bytes { ByteBuffer }import std.ioByteBuffer.allocate(0, -1)io.writeLine("after")
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения run. Ненулевой код завершения.
byte-buffer-byte-value at
main.tpz:3:1
Validate the initial byte of an empty buffer
Ссылкаimport std.bytes { ByteBuffer }import std.ioByteBuffer.allocate(0, 256)io.writeLine("after")
Нет стандартного вывода.Ожидаемый результат
Ожидаемый результат
Этап отклонения run. Ненулевой код завершения.
byte-buffer-byte-value at
main.tpz:3:1