pub struct Entry<T>(/* private fields */);Expand description
A shared handle to a metadata entry (a type, method, field, …).
Cloning an Entry is cheap and produces another handle to the same value.
Entries compare and hash by identity.
Implementations§
Source§impl<T> Entry<T>
impl<T> Entry<T>
pub fn new(value: T) -> Self
Sourcepub fn get(&self) -> ReadGuard<'_, T>
pub fn get(&self) -> ReadGuard<'_, T>
Borrows the value. Panics if it is currently mutably borrowed.
Examples found in repository?
examples/load_module.rs (line 18)
10fn main() {
11 let path = std::env::args()
12 .nth(1)
13 .expect("usage: load_module <assembly>");
14 let module = Module::from_path(&path).unwrap();
15 println!("loaded {}", module.name());
16
17 for ty in module.all_types() {
18 let ty = ty.get();
19 println!("{} {}.{}", ty.kind().keyword(), ty.namespace, ty.name);
20
21 // Decode compile-time constants (enum members and `const` fields) into a
22 // typed value, rather than hand-reading the raw blob.
23 for field in &ty.fields {
24 let field = field.get();
25 if let Some(value) = field.constant.as_ref().and_then(Constant::decode) {
26 println!(" {} = {value}", field.name);
27 }
28 }
29 }
30}More examples
examples/patch_method.rs (line 27)
14fn main() -> hao::error::Result<()> {
15 let mut args = std::env::args().skip(1);
16 let (input, output) = match (args.next(), args.next()) {
17 (Some(i), Some(o)) => (i, o),
18 _ => panic!("usage: patch_method <Sample.dll> <output.dll>"),
19 };
20 let mut module = Module::from_path_no_resolve(&input)?;
21
22 // A common BCL member whose signature is known: one call, no hand-written sig.
23 let write_line =
24 module.import_well_known_method(WellKnownMethod::Console(ConsoleMethod::WriteLineStr))?;
25
26 let add = module.find_method("Sample.Calc", "Add").expect("Calc.Add");
27 println!("before:\n{}", add.get().body.as_ref().unwrap().csharp());
28
29 add.edit_body(|body| {
30 // a + b -> a * b
31 let index = body.position(|i| i.opcode == OpCode::Add).unwrap();
32 body.instructions[index] = Instruction::simple(OpCode::Mul);
33
34 // Console.WriteLine("Add called") at the start of the method.
35 body.insert(
36 0,
37 [
38 Instruction::ldstr("Add called"),
39 Instruction::call(write_line),
40 ],
41 );
42 });
43 println!("after:\n{}", add.get().body.as_ref().unwrap().csharp());
44
45 let main = module.entry_point.clone().expect("entry point");
46 main.edit_body(|body| {
47 body.replace_operand(
48 &"Hello from hao".into(),
49 "Hello from a patched assembly".into(),
50 )
51 });
52
53 if let Some(dir) = std::path::Path::new(&output).parent() {
54 std::fs::create_dir_all(dir).ok();
55 }
56 module.write_to_path(&output)?;
57 println!("wrote {}", output);
58 Ok(())
59}Sourcepub fn get_mut(&self) -> WriteGuard<'_, T>
pub fn get_mut(&self) -> WriteGuard<'_, T>
Mutably borrows the value. Panics if it is currently borrowed.
pub fn try_get(&self) -> Option<ReadGuard<'_, T>>
pub fn try_get_mut(&self) -> Option<WriteGuard<'_, T>>
Sourcepub fn rid(&self) -> u32
pub fn rid(&self) -> u32
The row this entry was loaded from, or 0 if it was created after loading.
pub fn ptr_eq(&self, other: &Self) -> bool
pub fn downgrade(&self) -> WeakEntry<T>
Source§impl Entry<TypeDef>
Builder methods that need the shared handle (to set each child’s
declaring_type back-reference to its parent), so they live on
Entry<TypeDef> rather than TypeDef.
impl Entry<TypeDef>
Builder methods that need the shared handle (to set each child’s
declaring_type back-reference to its parent), so they live on
Entry<TypeDef> rather than TypeDef.
Source§impl Entry<Method>
impl Entry<Method>
Sourcepub fn edit_body<R>(&self, f: impl FnOnce(&mut MethodBody) -> R) -> Option<R>
pub fn edit_body<R>(&self, f: impl FnOnce(&mut MethodBody) -> R) -> Option<R>
Runs f on the method body. Returns None if the method has no body.
Examples found in repository?
examples/patch_method.rs (lines 29-42)
14fn main() -> hao::error::Result<()> {
15 let mut args = std::env::args().skip(1);
16 let (input, output) = match (args.next(), args.next()) {
17 (Some(i), Some(o)) => (i, o),
18 _ => panic!("usage: patch_method <Sample.dll> <output.dll>"),
19 };
20 let mut module = Module::from_path_no_resolve(&input)?;
21
22 // A common BCL member whose signature is known: one call, no hand-written sig.
23 let write_line =
24 module.import_well_known_method(WellKnownMethod::Console(ConsoleMethod::WriteLineStr))?;
25
26 let add = module.find_method("Sample.Calc", "Add").expect("Calc.Add");
27 println!("before:\n{}", add.get().body.as_ref().unwrap().csharp());
28
29 add.edit_body(|body| {
30 // a + b -> a * b
31 let index = body.position(|i| i.opcode == OpCode::Add).unwrap();
32 body.instructions[index] = Instruction::simple(OpCode::Mul);
33
34 // Console.WriteLine("Add called") at the start of the method.
35 body.insert(
36 0,
37 [
38 Instruction::ldstr("Add called"),
39 Instruction::call(write_line),
40 ],
41 );
42 });
43 println!("after:\n{}", add.get().body.as_ref().unwrap().csharp());
44
45 let main = module.entry_point.clone().expect("entry point");
46 main.edit_body(|body| {
47 body.replace_operand(
48 &"Hello from hao".into(),
49 "Hello from a patched assembly".into(),
50 )
51 });
52
53 if let Some(dir) = std::path::Path::new(&output).parent() {
54 std::fs::create_dir_all(dir).ok();
55 }
56 module.write_to_path(&output)?;
57 println!("wrote {}", output);
58 Ok(())
59}Trait Implementations§
impl<T> Eq for Entry<T>
Source§impl From<Entry<MethodSpec>> for MemberToken
impl From<Entry<MethodSpec>> for MemberToken
Source§fn from(value: Entry<MethodSpec>) -> Self
fn from(value: Entry<MethodSpec>) -> Self
Converts to this type from the input type.
Source§impl<T: WriteCSharp> WriteCSharp for Entry<T>
impl<T: WriteCSharp> WriteCSharp for Entry<T>
Auto Trait Implementations§
impl<T> Forget for Entry<T>
impl<T> Freeze for Entry<T>where
*const ArcInner<EntryInner<T>> is !null: Freeze,
PhantomData<ArcInner<EntryInner<T>>>: Freeze,
impl<T> Move for Entry<T>
impl<T> RefUnwindSafe for Entry<T>
impl<T> Send for Entry<T>
impl<T> Sync for Entry<T>
impl<T> Unpin for Entry<T>
impl<T> UnsafeUnpin for Entry<T>where
*const ArcInner<EntryInner<T>> is !null: UnsafeUnpin,
PhantomData<ArcInner<EntryInner<T>>>: UnsafeUnpin,
impl<T> UnwindSafe for Entry<T>where
Arc<EntryInner<T>>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more