pub trait Accessor {
// Required methods
fn data_map(&self) -> &HashMap<String, Value>;
fn data_map_mut(&mut self) -> &mut HashMap<String, Value>;
fn accessor_cache(&self) -> &HashMap<String, Value>;
fn accessor_cache_mut(&mut self) -> &mut HashMap<String, Value>;
// Provided methods
fn real_field_name(&self, name: &str) -> String { ... }
fn accessor_for(&self, field: &str, value: Option<&Value>) -> Value { ... }
fn get_attr(&mut self, name: &str) -> Value { ... }
fn get_data(&self, name: &str) -> Option<&Value> { ... }
fn has_attr(&mut self, name: &str) -> bool { ... }
}Expand description
访问器 trait — 对齐 PHP getAttr / getXxxAttr
§PHP 对齐
| PHP 方法 | Rust 等价 | 说明 |
|---|---|---|
getAttr($name) | Self::get_attr() | 入口方法,含缓存 |
getData($name) | Self::get_data() | 取原始字段值(不触发访问器) |
getXxxAttr($value, $data) | Self::accessor_for() | 业务模型按字段派发 |
getRealFieldName($name) | Self::real_field_name() | 字段名归一化 |
__isset($name) | Self::has_attr() | 触发访问器判 null |
$this->data | Self::data_map() | 原始字段数组 |
$this->get | Self::accessor_cache() | 访问器结果缓存 |
§缓存机制
- 首次
get_attr(field)触发访问器,结果缓存到accessor_cache - 同名字段被
set_attr时失效对应缓存 - PHP bug 复刻:不同名字段修改不失效派生字段缓存
(如
set_attr("status", ...)不失效status_text缓存)
Required Methods§
Sourcefn data_map_mut(&mut self) -> &mut HashMap<String, Value>
fn data_map_mut(&mut self) -> &mut HashMap<String, Value>
取可变原始字段数组
Sourcefn accessor_cache(&self) -> &HashMap<String, Value>
fn accessor_cache(&self) -> &HashMap<String, Value>
取访问器缓存(对应 PHP $this->get)
Sourcefn accessor_cache_mut(&mut self) -> &mut HashMap<String, Value>
fn accessor_cache_mut(&mut self) -> &mut HashMap<String, Value>
取可变访问器缓存
Provided Methods§
Sourcefn real_field_name(&self, name: &str) -> String
fn real_field_name(&self, name: &str) -> String
字段名归一化(对应 PHP getRealFieldName)
PHP 默认 $strict=true, $convertNameToCamel=false → 原样返回。
业务模型按需重写以支持 snake_case ↔ camelCase 转换。
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".