Skip to content

Commit bf01b32

Browse files
committed
doc: Using README for crate documentation and fix intra-crate links
1 parent 72d7151 commit bf01b32

File tree

3 files changed

+39
-76
lines changed

3 files changed

+39
-76
lines changed

README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ The current supported platforms are:
1010
- OSX
1111
- Linux
1212

13+
Some examples of use cases for this tool are:
14+
- Remote debugging tools
15+
- Game "trainers"
16+
- Rust clones of Cheat Engine
17+
1318
## Examples
1419
```rust
15-
use process_memory::{Memory, DataMember, Pid, TryIntoProcessHandle};
20+
# use process_memory::{Memory, DataMember, Pid, TryIntoProcessHandle};
1621
// We have a variable with some value
1722
let x = 4_u32;
1823
println!("Original x-value: {}", x);
@@ -26,15 +31,15 @@ println!("Memory location: &x: {}, member: {}", &x as *const _ as usize,
2631
member.get_offset().unwrap());
2732
assert_eq!(&x as *const _ as usize, member.get_offset().unwrap());
2833
// The value of the member is the same as the variable
29-
println!("Member value: {}", member.read().unwrap());
30-
assert_eq!(x, member.read().unwrap());
34+
println!("Member value: {}", unsafe { member.read().unwrap() });
35+
assert_eq!(x, unsafe { member.read().unwrap() });
3136
// We can write to and modify the value of the variable using the member
3237
member.write(&6_u32).unwrap();
3338
println!("New x-value: {}", x);
3439
assert_eq!(x, 6_u32);
3540
```
3641
```rust
37-
use process_memory::{Memory, LocalMember};
42+
# use process_memory::{Memory, LocalMember};
3843
// We have a variable with some value
3944
let x = 4_u32;
4045
println!("Original x-value: {}", x);
@@ -46,10 +51,27 @@ println!("Memory location: &x: {}, member: {}", &x as *const _ as usize,
4651
member.get_offset().unwrap());
4752
assert_eq!(&x as *const _ as usize, member.get_offset().unwrap());
4853
// The value of the member is the same as the variable
49-
println!("Member value: {}", member.read().unwrap());
50-
assert_eq!(x, member.read().unwrap());
54+
println!("Member value: {}", unsafe { member.read().unwrap() });
55+
assert_eq!(x, unsafe { member.read().unwrap() });
5156
// We can write to and modify the value of the variable using the member
5257
member.write(&6_u32).unwrap();
5358
println!("New x-value: {}", x);
5459
assert_eq!(x, 6_u32);
5560
```
61+
```no_run
62+
# use process_memory::{Architecture, Memory, DataMember, Pid, ProcessHandleExt, TryIntoProcessHandle};
63+
# fn get_pid(process_name: &str) -> Pid {
64+
# std::process::id() as Pid
65+
# }
66+
// We get a handle for a target process with a different architecture to ourselves
67+
let handle = get_pid("32Bit.exe").try_into_process_handle().unwrap()
68+
.set_arch(Architecture::Arch32Bit);
69+
// We make a `DataMember` that has a series of offsets refering to a known value in
70+
// the target processes memory
71+
let member = DataMember::new_offset(handle, vec![0x01_02_03_04, 0x04, 0x08, 0x10]);
72+
// The memory offset can now be correctly calculated:
73+
println!("Target memory location: {}", member.get_offset().unwrap());
74+
// The memory offset can now be used to retrieve and modify values:
75+
println!("Current value: {}", unsafe { member.read().unwrap() });
76+
member.write(&123_u32).unwrap();
77+
```

src/lib.rs

Lines changed: 9 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,4 @@
1-
//! This crate provides tools for working with the raw memory of programs.
2-
//!
3-
//! Some examples of use cases for this tool are:
4-
//! - Remote debugging tools
5-
//! - Game "trainers"
6-
//! - Rust clones of Cheat Engine
7-
//!
8-
//! ## Examples
9-
//! ```rust
10-
//! # use process_memory::{Memory, DataMember, Pid, TryIntoProcessHandle};
11-
//! // We have a variable with some value
12-
//! let x = 4_u32;
13-
//! println!("Original x-value: {}", x);
14-
//!
15-
//! // We need to make sure that we get a handle to a process, in this case, ourselves
16-
//! let handle = (std::process::id() as Pid).try_into_process_handle().unwrap();
17-
//! // We make a `DataMember` that has an offset referring to its location in memory
18-
//! let member = DataMember::new_offset(handle, vec![&x as *const _ as usize]);
19-
//! // The memory refered to is now the same
20-
//! println!("Memory location: &x: {}, member: {}", &x as *const _ as usize,
21-
//! member.get_offset().unwrap());
22-
//! assert_eq!(&x as *const _ as usize, member.get_offset().unwrap());
23-
//! // The value of the member is the same as the variable
24-
//! println!("Member value: {}", unsafe { member.read().unwrap() });
25-
//! assert_eq!(x, unsafe { member.read().unwrap() });
26-
//! // We can write to and modify the value of the variable using the member
27-
//! member.write(&6_u32).unwrap();
28-
//! println!("New x-value: {}", x);
29-
//! assert_eq!(x, 6_u32);
30-
//! ```
31-
//! ```rust
32-
//! # use process_memory::{Memory, LocalMember};
33-
//! // We have a variable with some value
34-
//! let x = 4_u32;
35-
//! println!("Original x-value: {}", x);
36-
//!
37-
//! // We make a `LocalMember` that has an offset referring to its location in memory
38-
//! let member = LocalMember::new_offset(vec![&x as *const _ as usize]);
39-
//! // The memory refered to is now the same
40-
//! println!("Memory location: &x: {}, member: {}", &x as *const _ as usize,
41-
//! member.get_offset().unwrap());
42-
//! assert_eq!(&x as *const _ as usize, member.get_offset().unwrap());
43-
//! // The value of the member is the same as the variable
44-
//! println!("Member value: {}", unsafe { member.read().unwrap() });
45-
//! assert_eq!(x, unsafe { member.read().unwrap() });
46-
//! // We can write to and modify the value of the variable using the member
47-
//! member.write(&6_u32).unwrap();
48-
//! println!("New x-value: {}", x);
49-
//! assert_eq!(x, 6_u32);
50-
//! ```
51-
//! ```no_run
52-
//! # use process_memory::{Architecture, Memory, DataMember, Pid, ProcessHandleExt, TryIntoProcessHandle};
53-
//! # fn get_pid(process_name: &str) -> Pid {
54-
//! # std::process::id() as Pid
55-
//! # }
56-
//! // We get a handle for a target process with a different architecture to ourselves
57-
//! let handle = get_pid("32Bit.exe").try_into_process_handle().unwrap()
58-
//! .set_arch(Architecture::Arch32Bit);
59-
//! // We make a `DataMember` that has a series of offsets refering to a known value in
60-
//! // the target processes memory
61-
//! let member = DataMember::new_offset(handle, vec![0x01_02_03_04, 0x04, 0x08, 0x10]);
62-
//! // The memory offset can now be correctly calculated:
63-
//! println!("Target memory location: {}", member.get_offset().unwrap());
64-
//! // The memory offset can now be used to retrieve and modify values:
65-
//! println!("Current value: {}", unsafe { member.read().unwrap() });
66-
//! member.write(&123_u32).unwrap();
67-
//! ```
1+
#![doc = include_str!("../README.md")]
682
#![deny(missing_docs)]
693
#![deny(unused_results)]
704
#![deny(unreachable_pub)]
@@ -109,6 +43,9 @@ pub trait CopyAddress {
10943
///
11044
/// # Errors
11145
/// `std::io::Error` if an error occurs copying the address.
46+
///
47+
/// [`copy_address`]: #tymethod.copy_address
48+
/// [`get_pointer_width`]: #tymethod.get_pointer_width
11249
fn get_offset(&self, offsets: &[usize]) -> std::io::Result<usize> {
11350
// Look ma! No unsafes!
11451
let mut offset: usize = 0;
@@ -131,6 +68,8 @@ pub trait CopyAddress {
13168
/// Any implementation of this function should be marked with
13269
/// `#[inline(always)]` as this function is *very* commonly called and
13370
/// should be inlined.
71+
///
72+
/// [`get_offset`]: #method.get_offset
13473
fn get_pointer_width(&self) -> Architecture;
13574
}
13675

@@ -180,9 +119,9 @@ impl TryIntoProcessHandle for ProcessHandle {
180119

181120
/// Additional functions on process handles
182121
pub trait ProcessHandleExt {
183-
/// Returns `true` if the `ProcessHandle` is not null, and `false` otherwise.
122+
/// Returns `true` if the [`ProcessHandle`] is not null, and `false` otherwise.
184123
fn check_handle(&self) -> bool;
185-
/// Return the null equivalent of a `ProcessHandle`.
124+
/// Return the null equivalent of a [`ProcessHandle`].
186125
#[must_use]
187126
fn null_type() -> ProcessHandle;
188127
/// Set this handle to use some architecture
@@ -253,7 +192,7 @@ pub trait Memory<T> {
253192

254193
/// Copy `length` bytes of memory at `addr` from `source`.
255194
///
256-
/// This is just a convenient way to call `CopyAddress::copy_address` without
195+
/// This is just a convenient way to call [`CopyAddress::copy_address`] without
257196
/// having to provide your own buffer.
258197
///
259198
/// # Errors

src/local_member.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ use crate::Memory;
3232
/// The implemented functions try to stop you from shooting yourself in the foot by checking none
3333
/// of the pointers end up at the null pointer, but this does not guarantee that you won't be able
3434
/// to mess something up really badly in your program.
35+
///
36+
/// [`DataMember`]: struct.DataMember.html
3537
#[derive(Clone, Debug, Default)]
3638
pub struct LocalMember<T> {
3739
offsets: Vec<usize>,

0 commit comments

Comments
 (0)