CodeSampleX

Exemple

block-buffer 0.12.1: block_buffer::LazyBuffer

Échantillon vérifié pour cargo block-buffer 0.12.1: block_buffer::LazyBuffer. Le contrat s'est exécuté sur rust 1 · linux alpine/x64 · docker et a réussi.

sha256:35194a67fa80405ebe2c48a129c93fba35ec2ee399457b00d223cb9700961707

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux x64 cargo

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-11-08

Cas

HOW
Objectif
verify block_buffer::LazyBuffer in pkg:cargo/block-buffer@0.12.1
Paquets
Symboles
  • block_buffer::LazyBuffer
Créé
2026-11-08T15:27:25Z

Contrat

  1. LazyBuffer::<U4>::try_new accepts slices up to len <= 4 (preserving get_pos() == len and remaining() == 4 - len) and rejects len > 4 with Error
  2. LazyBuffer::<U4>::digest_blocks retains one full 4-byte block in the buffer (get_pos() == 4, remaining() == 0) until at least one subsequent byte arrives
  3. LazyBuffer::<U4>::serialize and deserialize round-trip the 5-byte SerializedBuffer<U4, Lazy> (1-byte pos prefix in 0..=4 plus 4 data bytes) and reject non-zero trailing bytes beyond pos with Error
  4. LazyBuffer::<U4>::pad_with_zeros returns the buffered block padded with zeros as Array<u8, U4> (or the full 4-byte block directly when get_pos() == 4) and resets get_pos() to 0

Fichiers

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/main.rs
  • tests/contract.rs

Télécharger l’artefact source (tar.gz)

Code source

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "block-buffer"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa"
dependencies = [
 "hybrid-array",
]

[[package]]
name = "hybrid-array"
version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17"
dependencies = [
 "typenum",
]

[[package]]
name = "sample-lazy-buffer"
version = "0.1.0"
dependencies = [
 "block-buffer",
]

[[package]]
name = "typenum"
version = "1.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20"
Cargo.toml
[package]
name = "sample-lazy-buffer"
version = "0.1.0"
edition = "2021"

[dependencies]
block-buffer = "=0.12.1"
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify block_buffer::LazyBuffer in pkg:cargo/block-buffer@0.12.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/block-buffer@0.12.1
Demonstrate these symbols/APIs:
  - block_buffer::LazyBuffer

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:4058fb1498563d0c0563b875f5a35c4cb2c3b461e9ae8a655aad79ade678d92c","contract":["LazyBuffer::\u003cU4\u003e::try_new accepts slices up to len \u003c= 4 (preserving get_pos() == len and remaining() == 4 - len) and rejects len \u003e 4 with Error","LazyBuffer::\u003cU4\u003e::digest_blocks retains one full 4-byte block in the buffer (get_pos() == 4, remaining() == 0) until at least one subsequent byte arrives","LazyBuffer::\u003cU4\u003e::serialize and deserialize round-trip the 5-byte SerializedBuffer\u003cU4, Lazy\u003e (1-byte pos prefix in 0..=4 plus 4 data bytes) and reject non-zero trailing bytes beyond pos with Error","LazyBuffer::\u003cU4\u003e::pad_with_zeros returns the buffered block padded with zeros as Array\u003cu8, U4\u003e (or the full 4-byte block directly when get_pos() == 4) and resets get_pos() to 0"],"goal":"verify block_buffer::LazyBuffer in pkg:cargo/block-buffer@0.12.1","kind":"HOW","packages":["pkg:cargo/block-buffer@0.12.1"],"schemaVersion":1,"symbols":["block_buffer::LazyBuffer"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/block-buffer@0.12.1"],"schemaVersion":1,"subject":"pkg:cargo/block-buffer@0.12.1","symbols":["block_buffer::LazyBuffer"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify block_buffer::LazyBuffer in pkg:cargo/block-buffer@0.12.1",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/block-buffer@0.12.1"
  ],
  "symbols": [
    "block_buffer::LazyBuffer"
  ]
}
src/main.rs
use block_buffer::array::{Array, typenum::U4};
use block_buffer::{Error, LazyBuffer};

fn verify_lazy_buffer() {
    // 1. Construction and try_new boundary: LazyBuffer::<U4> allows slices up to 4 bytes (0..=4)
    let empty = LazyBuffer::<U4>::default();
    assert_eq!(empty.size(), 4);
    assert_eq!(empty.get_pos(), 0);
    assert_eq!(empty.remaining(), 4);
    assert_eq!(empty.get_data(), b"");

    let full_init = LazyBuffer::<U4>::try_new(b"abcd").expect("len == 4 is valid for LazyBuffer::<U4>");
    assert_eq!(full_init.get_pos(), 4);
    assert_eq!(full_init.remaining(), 0);
    assert_eq!(full_init.get_data(), b"abcd");

    assert_eq!(LazyBuffer::<U4>::try_new(b"abcde").unwrap_err(), Error);

    // 2. Lazy block digestion: retains one full block until at least one additional byte arrives
    let mut buf = LazyBuffer::<U4>::default();
    let mut emitted: Vec<[u8; 4]> = Vec::new();
    buf.digest_blocks(b"1234", |blocks| {
        for block in blocks {
            emitted.push((*block).into());
        }
    });
    assert!(emitted.is_empty());
    assert_eq!(buf.get_pos(), 4);
    assert_eq!(buf.remaining(), 0);
    assert_eq!(buf.get_data(), b"1234");

    buf.digest_blocks(b"5678", |blocks| {
        for block in blocks {
            emitted.push((*block).into());
        }
    });
    assert_eq!(emitted, vec![*b"1234"]);
    assert_eq!(buf.get_pos(), 4);
    assert_eq!(buf.get_data(), b"5678");

    buf.digest_blocks(b"9", |blocks| {
        for block in blocks {
            emitted.push((*block).into());
        }
    });
    assert_eq!(emitted, vec![*b"1234", *b"5678"]);
    assert_eq!(buf.get_pos(), 1);
    assert_eq!(buf.remaining(), 3);
    assert_eq!(buf.get_data(), b"9");

    // 3. Serialization and deserialization (Overhead = U1, so SerializedBuffer<U4, Lazy> is 5 bytes)
    let ser = buf.serialize();
    assert_eq!(ser.as_slice(), &[1, b'9', 0, 0, 0]);
    let restored = LazyBuffer::<U4>::deserialize(&ser).expect("valid lazy serialization");
    assert_eq!(restored.get_pos(), 1);
    assert_eq!(restored.get_data(), b"9");

    let full_ser = full_init.serialize();
    assert_eq!(full_ser.as_slice(), &[4, b'a', b'b', b'c', b'd']);
    let full_restored = LazyBuffer::<U4>::deserialize(&full_ser).expect("valid full lazy serialization");
    assert_eq!(full_restored.get_pos(), 4);
    assert_eq!(full_restored.get_data(), b"abcd");

    let mut invalid_ser = ser;
    invalid_ser[4] = 1; // non-zero trailing byte beyond pos must fail
    assert_eq!(LazyBuffer::<U4>::deserialize(&invalid_ser).unwrap_err(), Error);

    // 4. Padding and reset operations
    let padded: Array<u8, U4> = buf.pad_with_zeros();
    assert_eq!(padded.as_slice(), &[b'9', 0, 0, 0]);
    assert_eq!(buf.get_pos(), 0);

    // When LazyBuffer holds a full 4-byte block, pad_with_zeros returns that full block directly
    let mut full_for_pad = LazyBuffer::<U4>::try_new(b"wxyz").unwrap();
    assert_eq!(full_for_pad.pad_with_zeros().as_slice(), b"wxyz");
    assert_eq!(full_for_pad.get_pos(), 0);

    // reset clears buffered state back to pos 0
    let mut to_reset = LazyBuffer::<U4>::try_new(b"ABCD").unwrap();
    assert_eq!(to_reset.get_pos(), 4);
    to_reset.reset();
    assert_eq!(to_reset.get_pos(), 0);
    assert_eq!(to_reset.remaining(), 4);
    assert_eq!(to_reset.get_data(), b"");
}

fn main() {
    verify_lazy_buffer();
}
tests/contract.rs
use block_buffer::array::typenum::U4;
use block_buffer::{Error, LazyBuffer};

#[test]
fn contract_lazy_buffer() {
    assert!(LazyBuffer::<U4>::try_new(b"abcd").is_ok());
    assert_eq!(LazyBuffer::<U4>::try_new(b"abcde").unwrap_err(), Error);

    let mut buf = LazyBuffer::<U4>::default();
    let mut blocks_out: Vec<[u8; 4]> = Vec::new();
    buf.digest_blocks(b"1234", |blocks| {
        for b in blocks {
            blocks_out.push((*b).into());
        }
    });
    assert!(blocks_out.is_empty());
    assert_eq!(buf.get_pos(), 4);
    assert_eq!(buf.remaining(), 0);

    buf.digest_blocks(b"5", |blocks| {
        for b in blocks {
            blocks_out.push((*b).into());
        }
    });
    assert_eq!(blocks_out, vec![*b"1234"]);
    assert_eq!(buf.get_pos(), 1);
    assert_eq!(buf.get_data(), b"5");

    let serialized = buf.serialize();
    assert_eq!(serialized.as_slice(), &[1, b'5', 0, 0, 0]);
    let deserialized = LazyBuffer::<U4>::deserialize(&serialized).unwrap();
    assert_eq!(deserialized.get_data(), b"5");

    let padded = buf.pad_with_zeros();
    assert_eq!(padded.as_slice(), &[b'5', 0, 0, 0]);
    assert_eq!(buf.get_pos(), 0);
}

Seeder d'origine

anonyme