如果一个 Trait 要被多种 struct 实现,其中某几个或某一类 struct 对这个 trait 的实现方式一样,就可以用到 Marker Trait 这种设计模式。开源的做虚拟机模块化的 vm-memory 仓库就用到了这种技巧。
rust
/// 普通的实现方式
struct MyRegion { /* ... */ }
// 😭 必须手动实现所有 Bytes 的方法
impl Bytes<MemoryRegionAddress> for MyRegion {
fn write(&self, buf: &[u8], addr: MemoryRegionAddress) -> Result<usize> {
let maddr = addr.raw_value() as usize;
self.as_volatile_slice()?.write(buf, maddr).map_err(Into::into)
}
fn read(&self, buf: &mut [u8], addr: MemoryRegionAddress) -> Result<usize> {
let maddr = addr.raw_value() as usize;
self.as_volatile_slice()?.read(buf, maddr).map_err(Into::into)
}
fn write_slice(&self, ...) { /* ... */ }
fn read_slice(&self, ...) { /* ... */ }
fn read_volatile_from(&self, ...) { /* ... */ }
// ... 还有 4-5 个方法要写
}
/// 😁 用上 Marker Trait
pub trait MemoryRegionBytes {}
impl<R: MemoryRegionBytes> Bytes<MemoryRegionAddress> for R {
// ← 为所有实现了 GuestMemoryRegionBytes 的类型
// 自动提供 Bytes 的默认实现!
fn write(&self, buf: &[u8], addr: MemoryRegionAddress) -> Result<usize> {
let maddr = addr.raw_value() as usize;
self.as_volatile_slice()? // ← 调用 GuestMemoryRegion 的方法
.write(buf, maddr)
.map_err(Into::into)
}
fn read(&self, buf: &mut [u8], addr: MemoryRegionAddress) -> Result<usize> {
let maddr = addr.raw_value() as usize;
self.as_volatile_slice()?
.read(buf, maddr)
.map_err(Into::into)
}
// ... 其他 6-7 个方法都自动实现了
}
impl MemoryRegionBytes for MyRegion {}