trait_kit/lib.rs
1// Copyright (c) 2026 Kirky.X
2//
3// Licensed under the MIT License
4// See LICENSE file in the project root for full license information.
5
6//! trait-kit — 模块标准接口与能力管理中心
7//!
8//! 提供模块定义标准接口和 Kit 能力管理中心的轻量实现。
9
10#![deny(unsafe_code)]
11#![warn(clippy::all, clippy::pedantic)]
12#![allow(clippy::module_name_repetitions)]
13#![doc = include_str!("../README.md")]
14
15pub mod core;
16pub mod kit;
17
18pub mod prelude;
19
20#[cfg(feature = "async")]
21pub use core::meta::AsyncAutoBuilder;
22#[cfg(feature = "async")]
23pub use kit::{AsyncKit, AsyncReady, AsyncUnbuilt};
24
25/// Shared test helpers for async test modules (`block_on` executor + `MockError`).
26///
27/// Extracted to deduplicate between `core::meta::async_tests` and
28/// `kit::async_kit::tests` (audit LOW-003). Gated on `async` feature because
29/// both consumer test mods are `#[cfg(all(test, feature = "async"))]`.
30#[cfg(all(test, feature = "async"))]
31pub(crate) mod test_helpers {
32 use std::future::Future;
33 use std::task::{self, Poll};
34
35 /// Minimal single-threaded `Future` executor for tests (no extra deps).
36 ///
37 /// Uses `Waker::noop()` (stable since 1.85) because the `async` feature
38 /// deliberately stays dep-free (no `tokio` / `futures` test runtime).
39 pub(crate) fn block_on<F: Future>(future: F) -> F::Output {
40 let waker = task::Waker::noop();
41 // `Context::from_waker` takes `&Waker`; clippy::needless_borrow is a
42 // false positive here (removing the `&` would be a type error).
43 #[allow(clippy::needless_borrow)]
44 let mut cx = task::Context::from_waker(&waker);
45 let mut future = std::pin::pin!(future);
46 loop {
47 match future.as_mut().poll(&mut cx) {
48 Poll::Ready(v) => return v,
49 Poll::Pending => std::hint::spin_loop(),
50 }
51 }
52 }
53
54 /// Mock error type for tests verifying `AsyncAutoBuilder` trait signatures.
55 #[derive(Debug, thiserror::Error)]
56 #[allow(dead_code, reason = "mock error type verifies trait signature only")]
57 pub(crate) enum MockError {
58 #[error("mock build failed: {0}")]
59 Failed(String),
60 }
61}