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
18#[cfg(feature = "i18n")]
19pub mod i18n;
20
21pub mod prelude;
22
23#[cfg(feature = "async")]
24pub use core::meta::AsyncAutoBuilder;
25#[cfg(feature = "async")]
26pub use kit::{AsyncKit, AsyncReady, AsyncUnbuilt};
27
28/// Shared test helpers for async test modules (`block_on` executor + `MockError`).
29///
30/// Extracted to deduplicate between `core::meta::async_tests` and
31/// `kit::async_kit::tests` (audit LOW-003). Gated on `async` feature because
32/// both consumer test mods are `#[cfg(all(test, feature = "async"))]`.
33#[cfg(all(test, feature = "async"))]
34pub(crate) mod test_helpers {
35 use std::future::Future;
36 use std::task::{self, Poll};
37
38 /// Minimal single-threaded `Future` executor for tests (no extra deps).
39 ///
40 /// Uses `Waker::noop()` (stable since 1.85) because the `async` feature
41 /// deliberately stays dep-free (no `tokio` / `futures` test runtime).
42 pub(crate) fn block_on<F: Future>(future: F) -> F::Output {
43 let waker = task::Waker::noop();
44 // `Context::from_waker` takes `&Waker`; clippy::needless_borrow is a
45 // false positive here (removing the `&` would be a type error).
46 #[allow(clippy::needless_borrow)]
47 let mut cx = task::Context::from_waker(&waker);
48 let mut future = std::pin::pin!(future);
49 loop {
50 match future.as_mut().poll(&mut cx) {
51 Poll::Ready(v) => return v,
52 Poll::Pending => std::hint::spin_loop(),
53 }
54 }
55 }
56
57 /// Mock error type for tests verifying `AsyncAutoBuilder` trait signatures.
58 #[derive(Debug, thiserror::Error)]
59 #[allow(dead_code, reason = "mock error type verifies trait signature only")]
60 pub(crate) enum MockError {
61 #[error("mock build failed: {0}")]
62 Failed(String),
63 }
64}