Skip to main content

ratatui_kit/
props.rs

1use std::any::TypeId;
2
3use ratatui_kit_macros::Props;
4
5// 组件属性 trait,所有可作为组件 props 的类型都需实现此 trait。
6//
7// - 推荐使用 `#[derive(Props)]` 自动实现。
8pub trait Props {}
9
10// 用于处理原始指针释放的trait
11// 通过类型擦除实现对未知类型的内存释放
12trait DropRaw {
13    fn drop_raw(&self, raw: *mut ());
14}
15
16// 类型擦除的具体实现
17// 使用PhantomData标记实际类型T
18struct DropRawImpl<T> {
19    _marker: std::marker::PhantomData<T>,
20}
21
22impl<T> DropRaw for DropRawImpl<T> {
23    // 实际执行内存释放的方法
24    // 将原始指针转换为Box后释放
25    fn drop_raw(&self, raw: *mut ()) {
26        unsafe {
27            let _ = Box::from_raw(raw as *mut T);
28        }
29    }
30}
31
32// 类型擦除容器
33// 支持存储任意类型的属性(owned/borrowed)
34// raw: 指向实际数据的原始指针
35// drop: 可选的drop处理函数(对于owned类型)
36// _marker: 生命周期标记
37pub struct AnyProps<'a> {
38    raw: *mut (),
39    type_id: TypeId,
40    drop: Option<Box<dyn DropRaw + 'a>>,
41    _marker: std::marker::PhantomData<&'a mut ()>,
42}
43
44impl<'a> AnyProps<'a> {
45    // 创建拥有所有权的AnyProps实例
46    // T: 实现Props trait的类型
47    pub(crate) fn owned<T>(props: T, type_id: TypeId) -> Self
48    where
49        T: Props + 'a,
50    {
51        // 将属性堆分配并转换为原始指针
52        let raw = Box::into_raw(Box::new(props));
53        Self {
54            raw: raw as *mut (),
55            type_id,
56            // 保存对应的drop处理实现
57            drop: Some(Box::new(DropRawImpl::<T> {
58                _marker: std::marker::PhantomData,
59            })),
60            _marker: std::marker::PhantomData,
61        }
62    }
63
64    // 创建借用的AnyProps实例
65    // 不持有所有权,不负责内存释放
66    pub(crate) fn borrowed<T: Props>(props: &'a mut T, type_id: TypeId) -> Self {
67        Self {
68            raw: props as *const _ as *mut (),
69            type_id,
70            drop: None, // 不负责内存释放
71            _marker: std::marker::PhantomData,
72        }
73    }
74
75    // 创建只读借用版本
76    // drop字段设为None表示不处理内存释放
77    pub(crate) fn borrow(&mut self) -> AnyProps<'_> {
78        Self {
79            raw: self.raw,
80            type_id: self.type_id,
81            drop: None,
82            _marker: std::marker::PhantomData,
83        }
84    }
85
86    // 不安全的下转型方法(不可变引用)
87    // 调用者必须确保实际类型与T匹配
88    pub(crate) unsafe fn downcast_ref_unchecked<T: Props>(&self, expected_type_id: TypeId) -> &T {
89        debug_assert_eq!(
90            self.type_id, expected_type_id,
91            "AnyProps type mismatch before immutable downcast"
92        );
93        unsafe { &*(self.raw as *const T) }
94    }
95
96    // 不安全的下转型方法(可变引用)
97    // 调用者必须确保实际类型与T匹配
98    pub(crate) unsafe fn downcast_mut_unchecked<T: Props>(
99        &mut self,
100        expected_type_id: TypeId,
101    ) -> &mut T {
102        debug_assert_eq!(
103            self.type_id, expected_type_id,
104            "AnyProps type mismatch before mutable downcast"
105        );
106        unsafe { &mut *(self.raw as *mut T) }
107    }
108}
109
110// 实现Drop trait用于资源释放
111impl Drop for AnyProps<'_> {
112    fn drop(&mut self) {
113        // 如果存在drop处理器,执行内存释放
114        if let Some(drop) = self.drop.take() {
115            drop.drop_raw(self.raw);
116        }
117    }
118}
119
120#[derive(Debug, Clone, Default, Props)]
121// 空属性类型,表示组件不需要任何 props。
122//
123// 可用于无参数组件或默认占位。
124pub struct NoProps;