pub trait OnionObjectExt:
GCTraceable<OnionObjectCell>
+ Debug
+ Send
+ Sync
+ 'static {
Show 33 methods
// Required methods
fn as_any(&self) -> &dyn Any;
fn upgrade(&self, collected: &mut Vec<GCArc<OnionObjectCell>>);
fn equals(&self, other: &OnionObject) -> Result<bool, RuntimeError>;
// Provided methods
fn to_integer(&self) -> Result<i64, RuntimeError> { ... }
fn to_float(&self) -> Result<f64, RuntimeError> { ... }
fn to_string(
&self,
ptrs: &Vec<*const OnionObject>,
) -> Result<String, RuntimeError> { ... }
fn to_bytes(&self) -> Result<Box<[u8]>, RuntimeError> { ... }
fn to_boolean(&self) -> Result<bool, RuntimeError> { ... }
fn repr(
&self,
ptrs: &Vec<*const OnionObject>,
) -> Result<String, RuntimeError> { ... }
fn type_of(&self) -> Result<String, RuntimeError> { ... }
fn len(&self) -> Result<OnionStaticObject, RuntimeError> { ... }
fn contains(&self, other: &OnionObject) -> Result<bool, RuntimeError> { ... }
fn apply(
&self,
value: &OnionObject,
) -> Result<OnionStaticObject, RuntimeError> { ... }
fn key_of(&self) -> Result<OnionStaticObject, RuntimeError> { ... }
fn value_of(&self) -> Result<OnionStaticObject, RuntimeError> { ... }
fn with_attribute(
&self,
key: &OnionObject,
f: &mut dyn FnMut(&OnionObject) -> Result<(), RuntimeError>,
) -> Result<(), RuntimeError> { ... }
fn binary_eq(&self, other: &OnionObject) -> Result<bool, RuntimeError> { ... }
fn binary_lt(&self, other: &OnionObject) -> Result<bool, RuntimeError> { ... }
fn binary_gt(&self, other: &OnionObject) -> Result<bool, RuntimeError> { ... }
fn binary_add(
&self,
other: &OnionObject,
) -> Result<OnionStaticObject, RuntimeError> { ... }
fn binary_sub(
&self,
other: &OnionObject,
) -> Result<OnionStaticObject, RuntimeError> { ... }
fn binary_mul(
&self,
other: &OnionObject,
) -> Result<OnionStaticObject, RuntimeError> { ... }
fn binary_div(
&self,
other: &OnionObject,
) -> Result<OnionStaticObject, RuntimeError> { ... }
fn binary_mod(
&self,
other: &OnionObject,
) -> Result<OnionStaticObject, RuntimeError> { ... }
fn binary_pow(
&self,
other: &OnionObject,
) -> Result<OnionStaticObject, RuntimeError> { ... }
fn binary_and(
&self,
other: &OnionObject,
) -> Result<OnionStaticObject, RuntimeError> { ... }
fn binary_or(
&self,
other: &OnionObject,
) -> Result<OnionStaticObject, RuntimeError> { ... }
fn binary_xor(
&self,
other: &OnionObject,
) -> Result<OnionStaticObject, RuntimeError> { ... }
fn binary_shl(
&self,
other: &OnionObject,
) -> Result<OnionStaticObject, RuntimeError> { ... }
fn binary_shr(
&self,
other: &OnionObject,
) -> Result<OnionStaticObject, RuntimeError> { ... }
fn unary_neg(&self) -> Result<OnionStaticObject, RuntimeError> { ... }
fn unary_plus(&self) -> Result<OnionStaticObject, RuntimeError> { ... }
fn unary_not(&self) -> Result<OnionStaticObject, RuntimeError> { ... }
}
Expand description
Onion 对象扩展 trait。
定义了所有自定义对象类型必须实现的接口,提供完整的对象行为规范。 支持类型内省、GC 管理、类型转换、运算操作等功能。
§核心功能分类
§类型内省与 GC 管理
as_any()
: 类型向下转换支持upgrade()
: GC 弱引用升级
§基础类型转换
to_integer()
,to_float()
,to_string()
等to_boolean()
: 布尔值转换repr()
: 调试表示type_of()
: 类型名称
§容器操作
len()
: 长度获取contains()
: 包含关系检查apply()
: 函数应用
§键值操作
key_of()
,value_of()
: 键值提取with_attribute()
: 属性访问
§比较操作
equals()
: 值相等性比较(必须实现)is_same()
: 引用相等性比较(必须实现)binary_eq()
,binary_lt()
,binary_gt()
: 二元比较
§算术运算
binary_add()
,binary_sub()
,binary_mul()
,binary_div()
: 四则运算binary_mod()
,binary_pow()
: 取模与幂运算
§逻辑运算
binary_and()
,binary_or()
,binary_xor()
: 位运算binary_shl()
,binary_shr()
: 位移运算
§一元运算
unary_neg()
: 负号运算unary_not()
: 逻辑非运算
§实现注意事项
- 所有方法都有默认的错误实现
- 只需要重写适用于特定类型的方法
- 必须实现
equals()
和is_same()
方法 - 所有实现都应该是线程安全的
§错误处理
大部分方法默认返回 InvalidType
或 InvalidOperation
错误,
具体类型应该重写相关方法提供正确的实现。