CodeSampleX

示例

http-body 1.1.0: http_body

已验证示例 — cargo http-body 1.1.0: http_body. contract 在 rust 1 · linux alpine/x64 · docker 上运行并通过: Frame::data creates a data frame holding a byte buffer with…

sha256:21903c1394077d99150f7a1ae31e2001edaaa0ec56a197394cf9fbe06a173a41

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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-17

案例

HOW
目标
verify http_body in pkg:cargo/http-body@1.1.0
包
符号
  • http_body
创建时间
2026-09-17T15:16:24Z

契约

  1. Frame::data creates a data frame holding a byte buffer with data flags
  2. Frame::map_data transforms the buffer in a data frame
  3. SizeHint tracks lower and upper bounds and exact size
  4. SizeHint addition combines bounds from two hints
  5. Custom types implement Body to stream frames
  6. String implements Body yielding a data frame and reporting exact 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 = "bytes"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"

[[package]]
name = "http"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0"
dependencies = [
 "bytes",
 "itoa",
]

[[package]]
name = "http-body"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c"
dependencies = [
 "bytes",
 "http",
]

[[package]]
name = "itoa"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"

[[package]]
name = "sample-http-body"
version = "1.0.0"
dependencies = [
 "http-body",
]
Cargo.toml
[package]
name = "sample-http-body"
version = "1.0.0"
edition = "2021"
publish = false

[dependencies]
http-body = "=1.1.0"

[[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 http_body in pkg:cargo/http-body@1.1.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/http-body@1.1.0
Demonstrate these symbols/APIs:
  - http_body

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:d9fd86a5b61a49aba9b60eb93418d5150858324109b63a1a53b72f0280c6c4b6","contract":["Frame::data creates a data frame holding a byte buffer with data flags","Frame::map_data transforms the buffer in a data frame","SizeHint tracks lower and upper bounds and exact size","SizeHint addition combines bounds from two hints","Custom types implement Body to stream frames","String implements Body yielding a data frame and reporting exact size"],"goal":"verify http_body in pkg:cargo/http-body@1.1.0","kind":"HOW","packages":["pkg:cargo/http-body@1.1.0"],"schemaVersion":1,"symbols":["http_body"]},"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/http-body@1.1.0"],"schemaVersion":1,"subject":"pkg:cargo/http-body@1.1.0","symbols":["http_body"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify http_body in pkg:cargo/http-body@1.1.0",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/http-body@1.1.0"
  ],
  "symbols": [
    "http_body"
  ]
}
src/lib.rs
//! Clean-room verification sample for http-body.

pub use http_body::*;
test/contract.rs
use http_body::{Body, Frame, SizeHint};
use std::convert::Infallible;
use std::pin::Pin;
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};

fn noop_waker() -> Waker {
    const VTABLE: RawWakerVTable = RawWakerVTable::new(
        |_| RawWaker::new(std::ptr::null(), &VTABLE),
        |_| {},
        |_| {},
        |_| {},
    );
    unsafe { Waker::from_raw(RawWaker::new(std::ptr::null(), &VTABLE)) }
}

struct StaticBody {
    data: Option<&'static [u8]>,
}

impl Body for StaticBody {
    type Data = &'static [u8];
    type Error = Infallible;

    fn poll_frame(
        mut self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
        if let Some(data) = self.data.take() {
            Poll::Ready(Some(Ok(Frame::data(data))))
        } else {
            Poll::Ready(None)
        }
    }

    fn is_end_stream(&self) -> bool {
        self.data.is_none()
    }

    fn size_hint(&self) -> SizeHint {
        match self.data {
            Some(data) => SizeHint::with_exact(data.len() as u64),
            None => SizeHint::with_exact(0),
        }
    }
}

fn main() {
    let waker = noop_waker();
    let mut cx = Context::from_waker(&waker);

    // 1. Frame::data creates a data frame holding a byte buffer with data flags
    let mut frame = Frame::data(&b"stream payload"[..]);
    assert!(frame.is_data());
    assert!(!frame.is_trailers());
    assert_eq!(frame.data_ref(), Some(&&b"stream payload"[..]));
    assert_eq!(frame.data_mut(), Some(&mut &b"stream payload"[..]));
    let data = frame.into_data().expect("expected data payload");
    assert_eq!(data, b"stream payload");

    // 2. Frame::map_data transforms the buffer in a data frame
    let frame = Frame::data(&b"abc"[..]);
    let mapped = frame.map_data(|bytes| bytes.len());
    assert_eq!(mapped.into_data().expect("expected mapped data"), 3);

    // 3. SizeHint tracks lower and upper bounds and exact size
    let mut hint = SizeHint::new();
    assert_eq!(hint.lower(), 0);
    assert_eq!(hint.upper(), None);
    assert_eq!(hint.exact(), None);
    hint.set_lower(10);
    hint.set_upper(20);
    assert_eq!(hint.lower(), 10);
    assert_eq!(hint.upper(), Some(20));
    assert_eq!(hint.exact(), None);
    hint.set_exact(15);
    assert_eq!(hint.lower(), 15);
    assert_eq!(hint.upper(), Some(15));
    assert_eq!(hint.exact(), Some(15));

    // 4. SizeHint addition combines bounds from two hints
    let h1 = SizeHint::with_exact(100);
    let h2 = SizeHint::with_exact(250);
    let combined = h1 + h2;
    assert_eq!(combined.exact(), Some(350));

    // 5. Custom types implement Body to stream frames
    let mut custom_body = StaticBody {
        data: Some(b"custom frame data"),
    };
    assert_eq!(custom_body.size_hint().exact(), Some(17));
    assert!(!custom_body.is_end_stream());

    let mut pinned = Pin::new(&mut custom_body);
    let first_poll = pinned.as_mut().poll_frame(&mut cx);
    match first_poll {
        Poll::Ready(Some(Ok(f))) => {
            assert!(f.is_data());
            assert_eq!(f.into_data().unwrap(), b"custom frame data");
        }
        _ => panic!("expected successful frame from custom body"),
    }
    assert!(pinned.is_end_stream());
    let second_poll = pinned.as_mut().poll_frame(&mut cx);
    assert!(matches!(second_poll, Poll::Ready(None)));

    // 6. String implements Body yielding a data frame and reporting exact size
    let mut string_body = String::from("hello http_body");
    assert_eq!(string_body.size_hint().exact(), Some(15));
    assert!(!string_body.is_end_stream());

    let mut pinned_str = Pin::new(&mut string_body);
    let poll_str = pinned_str.as_mut().poll_frame(&mut cx);
    match poll_str {
        Poll::Ready(Some(Ok(f))) => {
            assert!(f.is_data());
            assert_eq!(f.into_data().unwrap().as_ref(), b"hello http_body");
        }
        _ => panic!("expected frame from String body"),
    }
    assert!(pinned_str.is_end_stream());
    let poll_str_end = pinned_str.as_mut().poll_frame(&mut cx);
    assert!(matches!(poll_str_end, Poll::Ready(None)));

    println!("All http_body contract assertions passed successfully.");
}

原始种子者

匿名