zstd Compress(level: int = 3) - > Result<Bytes, string>- Module
- std.
bytes - Receiver
- Bytes
- Minimum arity
- 0
- 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· 1272 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 52 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1272 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 518 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296 - compiler/crates/topaz_value/src/value/members.rs at
b48527d0a174· 243 - compiler/crates/topaz_value/src/value/builtin_catalog.rs at
b48527d0a174· 1109
Receiver
Bytes
Parameter constraints
Bytes.zstdCompress accepts an int level from 1 through 22. The default is 3 and other values return Err with a string.
The level is a compression effort hint. The API does not promise a size ratio, execution time, or different bytes for every pair of levels.
A successful call returns Ok with one RFC 8878 frame requiring no external dictionary. Empty input is valid.
Bytes.zstdDecompress accepts one standard frame containing raw, RLE, or compressed blocks, with valid frame parameters and an optional content checksum.
A frame requiring an unavailable dictionary, a malformed header or block, a size or checksum mismatch, and truncation return Err with a string.
Skippable frames, additional frames, and other trailing bytes return Err. The entire input must represent one data frame.
Decompression returns all uncompressed bytes in Ok or an error without a partial output. Compressing then decompressing preserves the input bytes.
The receiver and explicit level expression evaluate once in that order. The input remains unchanged and the operation performs no I/O.
Related rule: Use Zstandard frames and explicit effort levelsResult
The declared result type is Result<Bytes, string>.
Effects
Failures
Result and effects
LinkReturn one Zstandard frame in Ok. The level ranges from 1 through 22 and defaults to 3.
Resolved contracts
LinkRelated entries
LinkUse Zstandard frames and explicit effort levels
LinkUse Zstandard frames and explicit effort levels
Bytes.zstdCompress accepts an int level from 1 through 22. The default is 3 and other values return Err with a string.
The level is a compression effort hint. The API does not promise a size ratio, execution time, or different bytes for every pair of levels.
A successful call returns Ok with one RFC 8878 frame requiring no external dictionary. Empty input is valid.
Bytes.zstdDecompress accepts one standard frame containing raw, RLE, or compressed blocks, with valid frame parameters and an optional content checksum.
A frame requiring an unavailable dictionary, a malformed header or block, a size or checksum mismatch, and truncation return Err with a string.
Skippable frames, additional frames, and other trailing bytes return Err. The entire input must represent one data frame.
Decompression returns all uncompressed bytes in Ok or an error without a partial output. Compressing then decompressing preserves the input bytes.
The receiver and explicit level expression evaluate once in that order. The input remains unchanged and the operation performs no I/O.
Why
A bounded level range keeps calls portable. A complete frame boundary and explicit dictionary failure make the returned bytes independent of hidden decoder state.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 718 - compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 760 - compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 799 -
topaz.stdlib.contract.bytes-zstdatworking-tree - topaz.decision.bytes-compression-streams.1 at
topaz.decisi
Use the default and both supported level boundaries
Linkimport std.ioimport std.testlet source = "hello".encodeUtf8()io.writeLine(test.assertOk(test.assertOk(source.zstdCompress()).zstdDecompress()).toHex())for level in ([1, 22]) {io.writeLine(test.assertOk(test.assertOk(source.zstdCompress(level)).zstdDecompress()).toHex())}for level in ([0, 23]) {match (source.zstdCompress(level)) {case Err(_) => io.writeLine("invalid level")case Ok(_) => io.writeLine("accepted")}}
68656c6c6f 68656c6c6f 68656c6c6f invalid level invalid level
Expected result
Expected result
Execution completes. Exit code 0.
68656c6c6f68656c6c6f68656c6c6finvalid levelinvalid level
Source evidence
- compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 718 - compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 760 - compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 799 -
topaz.stdlib.contract.bytes-zstdatworking-tree - topaz.decision.bytes-compression-streams.1 at
topaz.decisi
Decode a valid Zstandard RLE block
Linkimport std.bytes { Bytes }import std.ioimport std.testlet encoded = test.assertOk(Bytes.fromHex("28b52ffd20052b000041"))match (encoded.zstdDecompress()) {case Ok(bytes) => io.writeLine(bytes.toHex())case Err(_) => io.writeLine("rejected")}
4141414141
Expected result
Expected result
Execution completes. Exit code 0.
4141414141
Source evidence
- compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 718 - compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 760 - compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 799 -
topaz.stdlib.contract.bytes-zstdatworking-tree - topaz.decision.bytes-compression-streams.1 at
topaz.decisi
Decode short nonfinal Zstandard raw blocks
Linkimport std.bytes { Bytes }import std.ioimport std.testlet encoded = test.assertOk(Bytes.fromHex("28b52ffd20020800004109000042"))match (encoded.zstdDecompress()) {case Ok(bytes) => io.writeLine(bytes.toHex())case Err(_) => io.writeLine("rejected")}
4142
Expected result
Expected result
Execution completes. Exit code 0.
4142
Source evidence
- compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 718 - compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 760 - compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 799 -
topaz.stdlib.contract.bytes-zstdatworking-tree - topaz.decision.bytes-compression-streams.1 at
topaz.decisi
Reject missing block data and trailing bytes
Linkimport std.bytes { Bytes }import std.ioimport std.testfor hex in (["", "28b52ffd20052900006869", "28b52ffd200001000000"]) {match (test.assertOk(Bytes.fromHex(hex)).zstdDecompress()) {case Err(_) => io.writeLine("invalid Zstandard")case Ok(_) => io.writeLine("accepted")}}
invalid Zstandard invalid Zstandard invalid Zstandard
Expected result
Expected result
Execution completes. Exit code 0.
invalid Zstandardinvalid Zstandardinvalid Zstandard
Source evidence
- compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 718 - compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 760 - compiler/crates/topaz_value/src/value/builtins/hash_codec.rs at
912a5e86e1f9· 799 -
topaz.stdlib.contract.bytes-zstdatworking-tree - topaz.decision.bytes-compression-streams.1 at
topaz.decisi