CodeSampleX

サンプル

equivalent 1.0.3: equivalent::Comparable, equivalent::Equivalent

検証済みサンプル — cargo equivalent 1.0.3: equivalent::Comparable, equivalent::Equivalent. rust 1 · linux alpine/x64 · docker で contract を実行し、成功しました.

sha256:5b08dcca1377296f917decc5dc86b8f38d56affc0d10f402e24a37e2b0efd6fb

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

実行証拠

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

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 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-11-05

ケース

HOW
ゴール
verify pkg:cargo/equivalent@1.0.3
パッケージ
シンボル
  • equivalent::Comparable
  • equivalent::Equivalent
作成日
2026-11-05T20:17:21Z

コントラクト

  1. Equivalent::equivalent returns true when a borrowed PairQuery matches an owned PairKey and false otherwise
  2. Comparable::compare orders a borrowed PairQuery against an owned PairKey lexicographically by namespace and name
  3. Blanket Equivalent and Comparable implementations on str and String report equality and Ordering::Equal

ファイル

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

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

ソース

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

[[package]]
name = "equivalent"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826"

[[package]]
name = "equivalent-sample"
version = "0.1.0"
dependencies = [
 "equivalent",
]
Cargo.toml
[package]
name = "equivalent-sample"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
equivalent = "=1.0.3"
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 pkg:cargo/equivalent@1.0.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/equivalent@1.0.3

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:6e9c8cd011bf9e4e41e7593374fa3d06596fa2b56a78cf1a0b439028a2b8eda7","contract":["Equivalent::equivalent returns true when a borrowed PairQuery matches an owned PairKey and false otherwise","Comparable::compare orders a borrowed PairQuery against an owned PairKey lexicographically by namespace and name","Blanket Equivalent and Comparable implementations on str and String report equality and Ordering::Equal"],"goal":"verify pkg:cargo/equivalent@1.0.3","kind":"HOW","packages":["pkg:cargo/equivalent@1.0.3"],"schemaVersion":1,"symbols":["equivalent::Comparable","equivalent::Equivalent"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/equivalent@1.0.3"],"schemaVersion":1,"subject":"pkg:cargo/equivalent@1.0.3","symbols":["equivalent::Comparable","equivalent::Equivalent"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:cargo/equivalent@1.0.3",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/equivalent@1.0.3"
  ]
}
src/lib.rs
use core::cmp::Ordering;
use equivalent::{Comparable, Equivalent};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct PairKey {
    pub namespace: String,
    pub name: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct PairQuery<'a> {
    pub namespace: &'a str,
    pub name: &'a str,
}

impl Equivalent<PairKey> for PairQuery<'_> {
    fn equivalent(&self, key: &PairKey) -> bool {
        self.namespace == key.namespace && self.name == key.name
    }
}

impl Comparable<PairKey> for PairQuery<'_> {
    fn compare(&self, key: &PairKey) -> Ordering {
        match self.namespace.cmp(&key.namespace) {
            Ordering::Equal => self.name.cmp(&key.name),
            ord => ord,
        }
    }
}

pub fn lookup_value<'v, Q>(entries: &'v [(PairKey, i32)], query: &Q) -> Option<i32>
where
    Q: Equivalent<PairKey> + ?Sized,
{
    entries
        .iter()
        .find(|(k, _)| query.equivalent(k))
        .map(|(_, v)| *v)
}

pub fn binary_search_value<Q>(sorted_entries: &[(PairKey, i32)], query: &Q) -> Option<i32>
where
    Q: Comparable<PairKey> + ?Sized,
{
    sorted_entries
        .binary_search_by(|(k, _)| query.compare(k).reverse())
        .ok()
        .map(|idx| sorted_entries[idx].1)
}
src/main.rs
use core::cmp::Ordering;
use equivalent::{Comparable, Equivalent};
use equivalent_sample::{binary_search_value, lookup_value, PairKey, PairQuery};

fn main() {
    let entries = vec![
        (
            PairKey {
                namespace: "alpha".to_string(),
                name: "first".to_string(),
            },
            10,
        ),
        (
            PairKey {
                namespace: "beta".to_string(),
                name: "second".to_string(),
            },
            20,
        ),
    ];

    let q_hit = PairQuery {
        namespace: "alpha",
        name: "first",
    };
    let q_miss = PairQuery {
        namespace: "alpha",
        name: "other",
    };

    assert!(q_hit.equivalent(&entries[0].0));
    assert!(!q_miss.equivalent(&entries[0].0));
    assert_eq!(q_hit.compare(&entries[0].0), Ordering::Equal);
    assert_eq!(q_hit.compare(&entries[1].0), Ordering::Less);

    assert_eq!(lookup_value(&entries, &q_hit), Some(10));
    assert_eq!(lookup_value(&entries, &q_miss), None);
    assert_eq!(binary_search_value(&entries, &q_hit), Some(10));
    assert_eq!(binary_search_value(&entries, &q_miss), None);

    let owned = String::from("hello");
    assert!(Equivalent::equivalent("hello", &owned));
    assert_eq!(Comparable::compare("hello", &owned), Ordering::Equal);
}
tests/contract.rs
use core::cmp::Ordering;
use equivalent::{Comparable, Equivalent};
use equivalent_sample::{binary_search_value, lookup_value, PairKey, PairQuery};

#[test]
fn contract_equivalent_and_comparable() {
    let entries = vec![
        (
            PairKey {
                namespace: "alpha".to_string(),
                name: "first".to_string(),
            },
            10,
        ),
        (
            PairKey {
                namespace: "beta".to_string(),
                name: "second".to_string(),
            },
            20,
        ),
    ];

    let q_hit = PairQuery {
        namespace: "alpha",
        name: "first",
    };
    let q_miss = PairQuery {
        namespace: "alpha",
        name: "other",
    };

    assert!(q_hit.equivalent(&entries[0].0));
    assert!(!q_miss.equivalent(&entries[0].0));
    assert_eq!(q_hit.compare(&entries[0].0), Ordering::Equal);
    assert_eq!(q_hit.compare(&entries[1].0), Ordering::Less);

    assert_eq!(lookup_value(&entries, &q_hit), Some(10));
    assert_eq!(lookup_value(&entries, &q_miss), None);
    assert_eq!(binary_search_value(&entries, &q_hit), Some(10));
    assert_eq!(binary_search_value(&entries, &q_miss), None);

    let owned = String::from("hello");
    assert!(Equivalent::equivalent("hello", &owned));
    assert_eq!(Comparable::compare("hello", &owned), Ordering::Equal);
}

オリジンシーダー

匿名