Skip to content

Commit

Permalink
Fix macro requiring Copy fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanieu committed Aug 16, 2019
1 parent 483d8d9 commit 89d7948
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/container_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ macro_rules! container_of {
if false {
// Ensure that the pointer has the correct type.
let $container { $field: _f, .. };
_f = *ptr;
_f = $crate::ptr::read(ptr);
}

// We don't use .sub because we need to support older Rust versions.
Expand Down Expand Up @@ -97,4 +97,19 @@ mod tests {
assert_eq!(container_of!(&x.1, Tup, 1), &x as *const _);
}
}

#[test]
fn non_copy() {
use core::cell::Cell;

#[repr(C)]
struct Foo {
a: Cell<u8>,
}

let x = Foo { a: Cell::new(0) };
unsafe {
assert_eq!(container_of!(&x.a, Foo, a), &x as *const _);
}
}
}

0 comments on commit 89d7948

Please sign in to comment.