Skip to content

Commit 6a30625

Browse files
authored
Implement fmt for AnyPtr (#652)
1 parent f614f07 commit 6a30625

4 files changed

Lines changed: 12 additions & 0 deletions

File tree

‎libcc2rs/src/void.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ impl PartialEq for AnyPtr {
133133
}
134134
}
135135

136+
impl std::fmt::Debug for AnyPtr {
137+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
138+
std::fmt::Debug::fmt(&self.ptr.as_bytes(), f)
139+
}
140+
}
141+
136142
impl AnyPtr {
137143
// char buffers are Ptr<i8>; access them directly rather than through a
138144
// reinterpreted byte view.

‎tests/unit/out/refcount/pointer_printf.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ fn main_0() -> i32 {
1414
let a: Value<i32> = Rc::new(RefCell::new(1));
1515
let mut p: Ptr<i32> = (a.as_pointer());
1616
print!("{:?} {}", p, (p.read()));
17+
let mut q: AnyPtr = (a.as_pointer()).to_any();
18+
println!(" {:?}", q);
1719
return 0;
1820
}
1921
pub fn __cpp2rust_init_globals() {}

‎tests/unit/out/unsafe/pointer_printf.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ unsafe fn main_0() -> i32 {
1616
let mut a: i32 = 1;
1717
let mut p: *mut i32 = (&mut a as *mut i32);
1818
(unsafe { libc::printf(c"%p %d".as_ptr() as *const libc::c_char, (p), (*p)) });
19+
let mut q: *mut ::libc::c_void = ((&mut a as *mut i32) as *mut ::libc::c_void);
20+
(unsafe { libc::printf(c" %p\n".as_ptr() as *const libc::c_char, (q)) });
1921
return 0;
2022
}
2123
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/pointer_printf.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ int main() {
33
int a = 1;
44
int *p = &a;
55
printf("%p %d", p, *p);
6+
void *q = &a;
7+
printf(" %p\n", q);
68
return 0;
79
}

0 commit comments

Comments
 (0)