AutoWrap / 自动封装
AutoWrap
A high-performance Rust library providing ergonomic smart pointer and interior mutability extensions with zero-cost abstractions.
Features
- Zero-Cost Abstractions: All methods are inlined at compile time with
#[inline(always)] - Cell Wrapper: Wrap a Copy type into a
Cell<T>for interior mutability. - RefCell Wrapper: Wrap a type into a
RefCell<T>for single-threaded interior mutability (std feature required). - Rc Wrapper: Wrap a type into an
Rc<T>for reference-counted shared ownership (std feature required). - Rc Wrapper: Wrap a type into
Rc<RefCell<T>>for shared, mutable ownership (std feature required). - Arc Wrapper: Wrap a type into
Arc<T>for thread-safe reference-counted ownership (sync feature required). - Arc Wrapper: Wrap a type into
Arc<Mutex<T>>for shared, mutable, thread-safe ownership (sync feature required). - Arc Wrapper: Wrap a type into
Arc<RwLock<T>>for shared, read-write lock ownership (sync feature required). - OnceLock Wrapper: Wrap a type into
OnceLock<T>for lazy initialization (sync feature required). - Atomic Wrappers: Wrap integer types into Atomic types with 7 conversion options (sync feature required).
Performance
AutoWrap is designed for maximum performance:
- Full LTO (Link Time Optimization): Cross-module inlining optimization
- Single Codegen Unit: Maximum optimization opportunities in release builds
- Zero Abstraction Overhead: All wrapper calls are inlined to direct constructor calls
- Macro-Generated Code: Reduced code duplication for better compiler optimization
Release Profile Optimization
[]
= 3 # Maximum optimization
= "fat" # Full Link Time Optimization
= 1 # Single unit for better optimization
= true # Remove debug symbols
= "abort" # Smaller binary size
Installation
Add to your Cargo.toml:
[]
= "1.0.1"
# Or with features:
[]
= "1.0.1"
= ["sync", "std"]
Usage
Basic Wrappers
use WrapExt;
// Interior mutability for Copy types
let cell = 42u32.cell;
cell.set;
assert_eq!;
// Reference counting
let rc = "hello".to_string.rc;
// Thread-safe sharing
let arc_mutex = 0u32.arc_mutex;
Thread-Safe Wrappers
use WrapExt;
// Arc<RwLock> for multiple readers/writers
let data = "hello".to_string.arc_rwlock;
// OnceLock for lazy initialization
let lazy = 42u32.once_lock;
Atomic Types
use AtomicWrapExt;
// Convert between atomic types easily
let atomic = 100u32.atomic_u32;
atomic.store;
assert_eq!;
// Cross-type conversion
let atomic_usize = 100u32.atomic_usize; // u32 -> AtomicUsize
Features Flags
std– Enable Rc, RefCell and related wrappers (enabled by default).sync– Enable Arc, Mutex, RwLock, OnceLock, and atomic wrappers (requires std).
License
MIT
自动封装
一个高性能的 Rust 库,提供人体工程学的智能指针和内部可变性扩展,具有零成本抽象。
特性
- 零成本抽象:所有方法在编译时使用
#[inline(always)]内联 - Cell 封装:将 Copy 类型封装为
Cell<T>,实现内部可变性。 - RefCell 封装:将类型封装为
RefCell<T>,用于单线程内部可变性(需要 std 特性)。 - Rc 封装:将类型封装为
Rc<T>,用于引用计数的共享所有权(需要 std 特性)。 - Rc 封装:将类型封装为
Rc<RefCell<T>>,用于共享可变所有权(需要 std 特性)。 - Arc 封装:将类型封装为
Arc<T>,用于线程安全的引用计数所有权(需要 sync 特性)。 - Arc 封装:将类型封装为
Arc<Mutex<T>>,用于共享可变线程安全所有权(需要 sync 特性)。 - Arc 封装:将类型封装为
Arc<RwLock<T>>,用于共享读写锁所有权(需要 sync 特性)。 - OnceLock 封装:将类型封装为
OnceLock<T>,用于延迟初始化(需要 sync 特性)。 - 原子类型封装:将整数类型封装为原子类型,支持 7 种转换选项(需要 sync 特性)。
性能
AutoWrap 经过专门设计,以实现最佳性能:
- 完整 LTO(链接时优化):跨模块内联优化
- 单一代码生成单元:发布版本中最大优化机会
- 零抽象开销:所有封装调用都内联为直接构造函数调用
- 宏生成代码:减少代码重复,提升编译器优化效果
发布配置优化
[]
= 3 # 最高优化级别
= "fat" # 完整链接时优化
= 1 # 单一单元以获得更好优化
= true # 移除调试符号
= "abort" # 更小的二进制体积
安装
添加到你的 Cargo.toml:
[]
= "1.1.0"
# 或使用特性:
[]
= "1.1.0"
= ["sync", "std"]
使用方法
基础封装
use WrapExt;
// Copy 类型的内部可变性
let cell = 42u32.cell;
cell.set;
assert_eq!;
// 引用计数
let rc = "hello".to_string.rc;
// 线程安全共享
let arc_mutex = 0u32.arc_mutex;
线程安全封装
use WrapExt;
// Arc<RwLock> 用于多读单写场景
let data = "hello".to_string.arc_rwlock;
// OnceLock 用于延迟初始化
let lazy = 42u32.once_lock;
原子类型
use AtomicWrapExt;
// 轻松转换原子类型
let atomic = 100u32.atomic_u32;
atomic.store;
assert_eq!;
// 跨类型转换
let atomic_usize = 100u32.atomic_usize; // u32 -> AtomicUsize
特性标志
std– 启用 Rc、RefCell 及相关封装(默认启用)。sync– 启用 Arc、Mutex、RwLock、OnceLock 和原子类型封装(需要 std)。
许可证
MIT