CodeSampleX

サンプル

zerocopy 0.8.56: zerocopy::Ref

検証済みサンプル — cargo zerocopy 0.8.56: zerocopy::Ref. rust 1 · linux alpine/x64 · docker で contract を実行し、成功しました: Ref::from_bytes parses an exact-size byte slice…

sha256:b2a09a2d55d066db1980d498f86de7f81c466f46536643018e8423d68ead7fea

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。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-09-21

ケース

HOW
ゴール
verify zerocopy::Ref in pkg:cargo/zerocopy@0.8.56
パッケージ
シンボル
  • zerocopy::Ref
作成日
2026-09-21T04:23:50Z

コントラクト

  1. Ref::from_bytes parses an exact-size byte slice into a typed Ref and provides access to fields via Deref
  2. Ref::into_ref converts Ref into a native immutable reference of the target type
  3. Ref::from_bytes on a mutable byte slice allows in-place mutation through DerefMut and Ref::into_mut
  4. Ref::from_prefix and Ref::from_suffix split byte slices into a Ref and remaining unparsed byte slices
  5. Ref::from_bytes returns CastError when the input slice length does not match the target type size

ファイル

  • 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"
version = "1.0.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.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954"

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

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

[dependencies]
zerocopy = { version = "=0.8.56", features = ["derive"] }

[[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::Ref in pkg:cargo/zerocopy@0.8.56
Kind: HOW

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

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:c2c50e7a31bcd211681fe9b998413369ae5503e3337db7f803e6591d4801c8ec","contract":["Ref::from_bytes parses an exact-size byte slice into a typed Ref and provides access to fields via Deref","Ref::into_ref converts Ref into a native immutable reference of the target type","Ref::from_bytes on a mutable byte slice allows in-place mutation through DerefMut and Ref::into_mut","Ref::from_prefix and Ref::from_suffix split byte slices into a Ref and remaining unparsed byte slices","Ref::from_bytes returns CastError when the input slice length does not match the target type size"],"goal":"verify zerocopy::Ref in pkg:cargo/zerocopy@0.8.56","kind":"HOW","packages":["pkg:cargo/zerocopy@0.8.56"],"schemaVersion":1,"symbols":["zerocopy::Ref"]},"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.56"],"schemaVersion":1,"subject":"pkg:cargo/zerocopy@0.8.56","symbols":["zerocopy::Ref"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify zerocopy::Ref in pkg:cargo/zerocopy@0.8.56",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/zerocopy@0.8.56"
  ],
  "symbols": [
    "zerocopy::Ref"
  ]
}
src/lib.rs
//! Clean-room verification sample for zerocopy.

pub use zerocopy::*;
test/contract.rs
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned};

#[derive(Clone, Copy, Debug, PartialEq, Eq, FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
#[repr(C)]
struct Header {
    magic: [u8; 4],
    version: u8,
    flags: u8,
    length: [u8; 2],
}

fn main() {
    let raw = [b'C', b'S', b'X', b'1', 1, 0, 0x00, 0x10];

    // 1. Ref::from_bytes parses an exact-size byte slice into a typed Ref and provides access to fields via Deref
    let r = Ref::<&[u8], Header>::from_bytes(&raw[..]).expect("from_bytes exact slice");
    assert_eq!(r.magic, *b"CSX1");
    assert_eq!(r.version, 1);
    assert_eq!(r.flags, 0);
    assert_eq!(r.length, [0x00, 0x10]);

    // 2. Ref::into_ref converts Ref into a native immutable reference of the target type
    let header_ref: &Header = Ref::into_ref(r);
    assert_eq!(header_ref.version, 1);
    assert_eq!(header_ref.length, [0x00, 0x10]);

    // 3. Ref::from_bytes on a mutable byte slice allows in-place mutation through DerefMut and Ref::into_mut
    let mut mut_buf = raw;
    {
        let mut r_mut = Ref::<&mut [u8], Header>::from_bytes(&mut mut_buf[..]).expect("from_bytes mut slice");
        r_mut.version = 2;
        let header_mut: &mut Header = Ref::into_mut(r_mut);
        header_mut.flags = 0xFF;
    }
    assert_eq!(mut_buf[4], 2);
    assert_eq!(mut_buf[5], 0xFF);

    // 4. Ref::from_prefix and Ref::from_suffix split byte slices into a Ref and remaining unparsed byte slices
    let extended = [b'C', b'S', b'X', b'1', 1, 0, 0, 0, 0xAA, 0xBB, 0xCC];
    let (r_prefix, suffix) = Ref::<&[u8], Header>::from_prefix(&extended[..]).expect("from_prefix");
    assert_eq!(r_prefix.magic, *b"CSX1");
    assert_eq!(suffix, &[0xAA, 0xBB, 0xCC]);

    let (prefix, r_suffix) = Ref::<&[u8], Header>::from_suffix(&extended[..]).expect("from_suffix");
    assert_eq!(prefix, &extended[..3]);
    assert_eq!(r_suffix.magic, [b'1', 1, 0, 0]);
    assert_eq!(r_suffix.version, 0);
    assert_eq!(r_suffix.flags, 0xAA);
    assert_eq!(r_suffix.length, [0xBB, 0xCC]);

    // 5. Ref::from_bytes returns CastError when the input slice length does not match the target type size
    let short = [0u8; 7];
    assert!(Ref::<&[u8], Header>::from_bytes(&short[..]).is_err());
    let long = [0u8; 9];
    assert!(Ref::<&[u8], Header>::from_bytes(&long[..]).is_err());

    println!("All zerocopy::Ref contract assertions passed successfully.");
}

オリジンシーダー

匿名