CodeSampleX

Пример

spin 0.9.9: spin::Lazy

Проверенный пример — cargo spin 0.9.9: spin::Lazy. Контракт выполнен на rust 1 · linux alpine/x64 · docker и пройден: spin::Lazy::new constructs an…

sha256:969b7f4eea2565a504ea955010286d6a671788a7fb838324d8e8ef082e605581

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. MIT-0

Свидетельства выполнения

Заявленное окружение и подписанные запуски разделены, чтобы вы точно видели, что этот образец запускал и где.

Основа свидетельства
Подписанный контракт пройден
Квитанции проверки
1
Ключи подписи, собравшие его
1
Заявленная среда linux x64 cargo

Среды запусков проверки

Окружение Контракт Этапы Запуск
rust 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-11-04

Кейс

HOW
Цель
verify spin::Lazy in pkg:cargo/spin@0.9.9
Пакеты
Символы
  • spin::Lazy
Создан
2026-11-04T15:27:32Z

Контракт

  1. spin::Lazy::new constructs an uninitialized lazy value without running the initialization closure until first access
  2. Dereferencing spin::Lazy or calling spin::Lazy::force runs the initialization closure once and returns a shared reference to the initialized value
  3. spin::Lazy::default creates a lazy value initialized via Default::default on first access

Файлы

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

Скачать артефакт с исходным кодом (tar.gz)

Исходный код

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

[[package]]
name = "lock_api"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965"
dependencies = [
 "scopeguard",
]

[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"

[[package]]
name = "spin"
version = "0.9.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e"
dependencies = [
 "lock_api",
]

[[package]]
name = "spin-lazy-sample"
version = "0.1.0"
dependencies = [
 "spin",
]
Cargo.toml
[package]
name = "spin-lazy-sample"
version = "0.1.0"
edition = "2021"

[dependencies]
spin = "=0.9.9"
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 spin::Lazy in pkg:cargo/spin@0.9.9
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/spin@0.9.9
Demonstrate these symbols/APIs:
  - spin::Lazy

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:7bd8fa419dcef2e651adff92600f397f33ac6a154c5ee79ffd4c17e15c389a84","contract":["spin::Lazy::new constructs an uninitialized lazy value without running the initialization closure until first access","Dereferencing spin::Lazy or calling spin::Lazy::force runs the initialization closure once and returns a shared reference to the initialized value","spin::Lazy::default creates a lazy value initialized via Default::default on first access"],"goal":"verify spin::Lazy in pkg:cargo/spin@0.9.9","kind":"HOW","packages":["pkg:cargo/spin@0.9.9"],"schemaVersion":1,"symbols":["spin::Lazy"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/spin@0.9.9"],"schemaVersion":1,"subject":"pkg:cargo/spin@0.9.9","symbols":["spin::Lazy"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify spin::Lazy in pkg:cargo/spin@0.9.9",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/spin@0.9.9"
  ],
  "symbols": [
    "spin::Lazy"
  ]
}
src/main.rs
use core::sync::atomic::{AtomicUsize, Ordering};
use spin::Lazy;

static INIT_COUNT: AtomicUsize = AtomicUsize::new(0);
static COMPUTED: Lazy<usize> = Lazy::new(|| {
    INIT_COUNT.fetch_add(1, Ordering::SeqCst);
    21 * 2
});

fn main() {
    // Before first access, the initializer closure has not run.
    assert_eq!(INIT_COUNT.load(Ordering::SeqCst), 0);

    // Dereferencing Lazy initializes the value once and returns a shared reference to it.
    assert_eq!(*COMPUTED, 42);
    assert_eq!(INIT_COUNT.load(Ordering::SeqCst), 1);

    // Calling Lazy::force returns a reference to the already-initialized value without re-running the initializer.
    assert_eq!(*Lazy::force(&COMPUTED), 42);
    assert_eq!(INIT_COUNT.load(Ordering::SeqCst), 1);

    // Lazy::default creates a Lazy that initializes with T::default() on first access.
    let default_lazy: Lazy<Vec<i32>> = Lazy::default();
    assert!(default_lazy.is_empty());
}

Исходный сидер

аноним