Wil*_*hin 4 generics types rust
我正在开发一个适用于32位和64位体系结构的PE文件的库。一些结构将同时引用虚拟地址(例如VA ImageBase)和相对虚拟地址(RVA例如节偏移),例如:
type VA32 = u32;
type RVA32 = i32;
struct Header32 {
image_base: VA32,
section_offsets: Vec<RVA32>,
}
let hdr = Header32 { /* ... */ };
Run Code Online (Sandbox Code Playgroud)
当处理32位PE文件时,VA应该是32位且无符号,而RVA应该是32位且有符号。对于64位PE文件,两种类型都应为64位。
我希望我的结构为这些类型使用适当的宽度,可能是通过使它们通用:
struct Header<VA, RVA> {
image_base: VA,
section_offsets: Vec<RVA>,
}
type VA32 = u32;
type RVA32 = i32;
let hdr: Header<VA32, RVA32> = Header { /* ... */ };
Run Code Online (Sandbox Code Playgroud)
但是VA32只有永远伴随下去RVA32,并且VA64应该永远被提供RVA64。有没有一种我可以表达的惯用方式?
我希望使用完整的语法,例如:
struct Header<Arch> {
image_base: arch.VA,
section_offsets: Vec<arch.RVA>,
}
type Arch32 = { VA: u32, RVA: i32 }
let hdr: Header<Arch32> = Header { /* ... */ };
Run Code Online (Sandbox Code Playgroud)
您可以使用具有关联类型和零大小结构的特征作为标记,来执行与组合语法相似的操作。
trait Arch {
type VA;
type RVA;
}
#[derive(Debug)]
struct Arch32;
impl Arch for Arch32 {
type VA = u32;
type RVA = i32;
}
#[derive(Debug)]
struct Arch64;
impl Arch for Arch64 {
type VA = u64;
type RVA = i64;
}
#[derive(Debug)]
struct Header<T: Arch> {
image_base: T::VA,
section_offsets: Vec<T::RVA>,
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |