CodeSampleX

示例

mio 1.2.2

已验证示例 — cargo mio 1.2.2. contract 在 rust 1 · linux alpine/x64 · docker 上运行并通过: Poll and Events initialize with capacity and handle empty poll timeout

sha256:513ec37fc0e29546b1f232d68b1904178b0f9699be29e2822ece8df28af1a22d

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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-19

案例

HOW
目标
verify pkg:cargo/mio@1.2.2
包
创建时间
2026-09-18T11:59:46Z

契约

  1. Poll and Events initialize with capacity and handle empty poll timeout
  2. Interest combines readable and writable readiness states
  3. Waker registers with Poll registry and wakes poll waiting on events
  4. TcpListener and TcpStream bind, register, and transmit bytes across non-blocking connection

文件

  • 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 = "libc"
version = "0.2.189"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"

[[package]]
name = "log"
version = "0.4.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6"

[[package]]
name = "mio"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30d65c71f1ce40ab09135ce117d742b9f8a19ff91a41a8b57ed50bc2de59c427"
dependencies = [
 "libc",
 "log",
 "wasi",
 "windows-sys",
]

[[package]]
name = "sample-mio"
version = "1.0.0"
dependencies = [
 "mio",
]

[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"

[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"

[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
 "windows-link",
]
Cargo.toml
[package]
name = "sample-mio"
version = "1.0.0"
edition = "2021"
publish = false

[dependencies]
mio = { version = "=1.2.2", features = ["os-poll", "net"] }

[[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 pkg:cargo/mio@1.2.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/mio@1.2.2

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:d62cebf97ea061788e8b51d37a8167ff9d622461beb505f4fef53fcdac9fbeee","contract":["Poll and Events initialize with capacity and handle empty poll timeout","Interest combines readable and writable readiness states","Waker registers with Poll registry and wakes poll waiting on events","TcpListener and TcpStream bind, register, and transmit bytes across non-blocking connection"],"goal":"verify pkg:cargo/mio@1.2.2","kind":"HOW","packages":["pkg:cargo/mio@1.2.2"],"schemaVersion":1},"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/mio@1.2.2"],"schemaVersion":1,"subject":"pkg:cargo/mio@1.2.2","verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:cargo/mio@1.2.2",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/mio@1.2.2"
  ]
}
src/lib.rs
//! Clean-room verification sample for mio.

pub use mio::*;
test/contract.rs
use mio::net::{TcpListener, TcpStream};
use mio::{Events, Interest, Poll, Token, Waker};
use std::io::{Read, Write};
use std::time::Duration;

fn main() {
    // 1. Poll and Events initialize with capacity and handle empty poll timeout
    let mut poll = Poll::new().expect("Failed to create Poll instance");
    let mut events = Events::with_capacity(64);
    assert!(events.is_empty());
    assert_eq!(events.capacity(), 64);

    poll.poll(&mut events, Some(Duration::from_millis(10)))
        .expect("Poll timed out or errored");
    assert!(events.is_empty());

    // 2. Interest combines readable and writable readiness states
    let read = Interest::READABLE;
    let write = Interest::WRITABLE;
    let combined = read.add(write);
    assert!(combined.is_readable());
    assert!(combined.is_writable());
    assert!(read.is_readable());
    assert!(!read.is_writable());
    assert!(!write.is_readable());
    assert!(write.is_writable());

    // 3. Waker registers with Poll registry and wakes poll waiting on events
    const WAKER_TOKEN: Token = Token(10);
    let waker = Waker::new(poll.registry(), WAKER_TOKEN).expect("Failed to create Waker");
    waker.wake().expect("Failed to wake Poll");

    poll.poll(&mut events, Some(Duration::from_millis(100)))
        .expect("Failed to poll for waker event");
    assert!(!events.is_empty());

    let mut waker_found = false;
    for event in events.iter() {
        if event.token() == WAKER_TOKEN && event.is_readable() {
            waker_found = true;
        }
    }
    assert!(waker_found, "Waker event was not observed");

    // 4. TcpListener and TcpStream bind, register, and transmit bytes across non-blocking connection
    const LISTENER_TOKEN: Token = Token(1);
    const CLIENT_TOKEN: Token = Token(2);
    const SERVER_STREAM_TOKEN: Token = Token(3);

    let mut listener = TcpListener::bind("127.0.0.1:0".parse().unwrap())
        .expect("Failed to bind TcpListener to loopback");
    let addr = listener.local_addr().expect("Failed to get local address");

    poll.registry()
        .register(&mut listener, LISTENER_TOKEN, Interest::READABLE)
        .expect("Failed to register TcpListener");

    let mut client = TcpStream::connect(addr).expect("Failed to connect TcpStream");
    poll.registry()
        .register(&mut client, CLIENT_TOKEN, Interest::WRITABLE | Interest::READABLE)
        .expect("Failed to register client TcpStream");

    let mut server_stream = None;
    let mut client_writable = false;

    for _ in 0..20 {
        events.clear();
        poll.poll(&mut events, Some(Duration::from_millis(100)))
            .expect("Failed to poll connection events");

        for event in events.iter() {
            if event.token() == LISTENER_TOKEN && event.is_readable() {
                if let Ok((stream, _)) = listener.accept() {
                    server_stream = Some(stream);
                }
            }
            if event.token() == CLIENT_TOKEN && event.is_writable() {
                client_writable = true;
            }
        }

        if server_stream.is_some() && client_writable {
            break;
        }
    }

    assert!(server_stream.is_some(), "Listener never accepted connection");
    let mut server = server_stream.unwrap();

    poll.registry()
        .register(&mut server, SERVER_STREAM_TOKEN, Interest::READABLE)
        .expect("Failed to register server stream");

    // Send data from client to server
    let payload = b"mio ping test";
    let n = client.write(payload).expect("Client failed to write bytes");
    assert_eq!(n, payload.len());

    let mut received = Vec::new();
    let mut buf = [0u8; 64];

    for _ in 0..20 {
        events.clear();
        poll.poll(&mut events, Some(Duration::from_millis(100)))
            .expect("Failed to poll read events");

        for event in events.iter() {
            if event.token() == SERVER_STREAM_TOKEN && event.is_readable() {
                match server.read(&mut buf) {
                    Ok(read_bytes) if read_bytes > 0 => {
                        received.extend_from_slice(&buf[..read_bytes]);
                    }
                    _ => {}
                }
            }
        }

        if received.len() >= payload.len() {
            break;
        }
    }

    assert_eq!(&received[..], payload, "Transmitted payload does not match received");

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

原始种子者

匿名