From ebb335a3c244f409bb9fe91bbc8e5bd3162f8f74 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Fri, 16 Aug 2019 14:24:43 +0200 Subject: [PATCH] Fix macro requiring Copy fields --- src/container_of.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/container_of.rs b/src/container_of.rs index a3ce796..bae50dc 100644 --- a/src/container_of.rs +++ b/src/container_of.rs @@ -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. @@ -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, + } + + let x = Foo { a: Cell::new(0) }; + unsafe { + assert_eq!(container_of!(&x.a, Foo, a), &x as *const _); + } + } }