CodeSampleX

サンプル

zerocopy 0.8.62: zerocopy::FromBytes

検証済みサンプル — cargo zerocopy 0.8.62: zerocopy::FromBytes. rust 1 · linux alpine/x64 · docker で contract を実行し、成功しました: FromBytes::read_from_bytes decodes a value…

sha256:497c5e778204550b282597bbfed5ecbc11ca6fedb946e1002c05deed4253f8c0

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux · alpine · musl x64 cargo

検証実行環境

環境 コントラクト ステージ 実行日
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-10-16

ケース

HOW
ゴール
verify zerocopy::FromBytes in pkg:cargo/zerocopy@0.8.62
パッケージ
シンボル
  • zerocopy::FromBytes
作成日
2026-10-16T16:48:07Z

コントラクト

  1. FromBytes::read_from_bytes decodes a value from an unaligned byte slice of exact length
  2. FromBytes::read_from_bytes returns an error when the byte slice length does not match
  3. FromBytes::read_from_prefix and FromBytes::read_from_suffix decode a value and return the remaining slice
  4. FromBytes::ref_from_bytes borrows an aligned byte buffer as a typed reference
  5. FromBytes::ref_from_prefix borrows the aligned prefix of a byte slice and returns the remainder
  6. FromBytes::ref_from_bytes_with_elems borrows an aligned byte buffer as a typed slice of fixed element count
  7. FromBytes::mut_from_bytes mutates an aligned byte buffer in place through a typed mutable reference

ファイル

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/lib.rs
  • test/contract.rs

ソースアーティファクトをダウンロード (tar.gz)

ソース

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

[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
 "unicode-ident",
]

[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "sample-zerocopy-frombytes"
version = "0.1.0"
dependencies = [
 "zerocopy",
]

[[package]]
name = "syn"
version = "2.0.119"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-ident",
]

[[package]]
name = "unicode-ident"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"

[[package]]
name = "zerocopy"
version = "0.8.62"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86502bf56ac7c77571a32e2647bb2a15894565e981fb2a48d7bde2d91c965a9d"
dependencies = [
 "zerocopy-derive",
]

[[package]]
name = "zerocopy-derive"
version = "0.8.62"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5457206954b06561e2608c7e19cf58b1926586d999c246eebe4502f7e2039d1a"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]
Cargo.toml
[package]
name = "sample-zerocopy-frombytes"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
zerocopy = "=0.8.62"

[[bin]]
name = "contract"
path = "test/contract.rs"
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 zerocopy::FromBytes in pkg:cargo/zerocopy@0.8.62
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/zerocopy@0.8.62
Demonstrate these symbols/APIs:
  - zerocopy::FromBytes

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:f798090b8afad5e4ca8d0b50f64ae6ce6b071251819864017a1714b36df5a63a","contract":["FromBytes::read_from_bytes decodes a value from an unaligned byte slice of exact length","FromBytes::read_from_bytes returns an error when the byte slice length does not match","FromBytes::read_from_prefix and FromBytes::read_from_suffix decode a value and return the remaining slice","FromBytes::ref_from_bytes borrows an aligned byte buffer as a typed reference","FromBytes::ref_from_prefix borrows the aligned prefix of a byte slice and returns the remainder","FromBytes::ref_from_bytes_with_elems borrows an aligned byte buffer as a typed slice of fixed element count","FromBytes::mut_from_bytes mutates an aligned byte buffer in place through a typed mutable reference"],"goal":"verify zerocopy::FromBytes in pkg:cargo/zerocopy@0.8.62","kind":"HOW","packages":["pkg:cargo/zerocopy@0.8.62"],"schemaVersion":1,"symbols":["zerocopy::FromBytes"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","distro":"alpine","ecosystem":"cargo","libc":"musl","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/zerocopy@0.8.62"],"schemaVersion":1,"subject":"pkg:cargo/zerocopy@0.8.62","symbols":["zerocopy::FromBytes"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify zerocopy::FromBytes in pkg:cargo/zerocopy@0.8.62",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/zerocopy@0.8.62"
  ],
  "symbols": [
    "zerocopy::FromBytes"
  ]
}
src/lib.rs
pub use zerocopy::FromBytes;
test/contract.rs
use zerocopy::FromBytes;

#[repr(align(4))]
struct Align4<T>(T);

#[repr(align(8))]
struct Align8<T>(T);

fn main() {
    // 1. read_from_bytes copies a value from an unaligned byte slice of exact size
    let raw = [0xFFu8, 0x01, 0x02, 0x03, 0x04];
    let parsed = u32::read_from_bytes(&raw[1..5]).expect("read_from_bytes must succeed on 4 bytes");
    assert_eq!(parsed, u32::from_ne_bytes([0x01, 0x02, 0x03, 0x04]));

    // 2. read_from_bytes rejects slices whose length does not match the target type size
    assert!(u32::read_from_bytes(&raw[1..4]).is_err());
    assert!(u32::read_from_bytes(&raw[..]).is_err());

    // 3. read_from_prefix and read_from_suffix split and decode a value from the start or end of a byte slice
    let frame = [0x10u8, 0x20, 0x30, 0x40, 0xAA, 0xBB];
    let (head, tail) = u32::read_from_prefix(&frame).expect("read_from_prefix must succeed");
    assert_eq!(head, u32::from_ne_bytes([0x10, 0x20, 0x30, 0x40]));
    assert_eq!(tail, &[0xAA, 0xBB]);

    let (init, last) = u16::read_from_suffix(&frame).expect("read_from_suffix must succeed");
    assert_eq!(init, &[0x10, 0x20, 0x30, 0x40]);
    assert_eq!(last, u16::from_ne_bytes([0xAA, 0xBB]));

    // 4. ref_from_bytes borrows an aligned byte slice as a shared reference without copying
    let aligned = Align4([0x0Au8, 0x0B, 0x0C, 0x0D]);
    let borrowed: &u32 = u32::ref_from_bytes(&aligned.0).expect("aligned ref_from_bytes must succeed");
    assert_eq!(*borrowed, u32::from_ne_bytes([0x0A, 0x0B, 0x0C, 0x0D]));

    // 5. ref_from_prefix borrows a prefix of an aligned byte slice as a reference and returns the remainder
    let aligned_frame = Align4([0x01u8, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
    let (prefix_ref, rem) = u32::ref_from_prefix(&aligned_frame.0).expect("ref_from_prefix must succeed");
    assert_eq!(*prefix_ref, u32::from_ne_bytes([0x01, 0x02, 0x03, 0x04]));
    assert_eq!(rem, &[0x05, 0x06, 0x07, 0x08]);

    // 6. ref_from_bytes_with_elems borrows an aligned slice of elements with a requested count
    let aligned_words = Align8([1u8, 0, 2, 0, 3, 0, 4, 0]);
    let slice_ref: &[u16] = <[u16]>::ref_from_bytes_with_elems(&aligned_words.0, 4)
        .expect("ref_from_bytes_with_elems must succeed");
    assert_eq!(slice_ref.len(), 4);
    assert_eq!(slice_ref[0], u16::from_ne_bytes([1, 0]));
    assert_eq!(slice_ref[3], u16::from_ne_bytes([4, 0]));

    // 7. mut_from_bytes borrows an aligned mutable byte slice and mutates the underlying bytes in place
    let mut buf = Align4([0u8; 4]);
    let slot: &mut u32 = u32::mut_from_bytes(&mut buf.0).expect("mut_from_bytes must succeed");
    *slot = u32::from_ne_bytes([0xDE, 0xAD, 0xBE, 0xEF]);
    assert_eq!(buf.0, [0xDE, 0xAD, 0xBE, 0xEF]);
}

オリジンシーダー

匿名