aboutsummaryrefslogtreecommitdiff
path: root/vendor/bytemuck/tests/offset_of_tests.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/bytemuck/tests/offset_of_tests.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/bytemuck/tests/offset_of_tests.rs')
-rw-r--r--vendor/bytemuck/tests/offset_of_tests.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/vendor/bytemuck/tests/offset_of_tests.rs b/vendor/bytemuck/tests/offset_of_tests.rs
new file mode 100644
index 0000000..4cad5d4
--- /dev/null
+++ b/vendor/bytemuck/tests/offset_of_tests.rs
@@ -0,0 +1,60 @@
+#![allow(clippy::disallowed_names)]
+use bytemuck::{offset_of, Zeroable};
+
+#[test]
+fn test_offset_of_vertex() {
+ #[repr(C)]
+ struct Vertex {
+ pos: [f32; 2],
+ uv: [u16; 2],
+ color: [u8; 4],
+ }
+ unsafe impl Zeroable for Vertex {}
+
+ let pos = offset_of!(Zeroable::zeroed(), Vertex, pos);
+ let uv = offset_of!(Zeroable::zeroed(), Vertex, uv);
+ let color = offset_of!(Zeroable::zeroed(), Vertex, color);
+
+ assert_eq!(pos, 0);
+ assert_eq!(uv, 8);
+ assert_eq!(color, 12);
+}
+
+#[test]
+fn test_offset_of_foo() {
+ #[derive(Default)]
+ struct Foo {
+ a: u8,
+ b: &'static str,
+ c: i32,
+ }
+
+ let a_offset = offset_of!(Default::default(), Foo, a);
+ let b_offset = offset_of!(Default::default(), Foo, b);
+ let c_offset = offset_of!(Default::default(), Foo, c);
+
+ assert_ne!(a_offset, b_offset);
+ assert_ne!(b_offset, c_offset);
+ // We can't check against hardcoded values for a repr(Rust) type,
+ // but prove to ourself this way.
+
+ let foo = Foo::default();
+ // Note: offsets are in bytes.
+ let as_bytes = &foo as *const _ as *const u8;
+
+ // we're using wrapping_offset here because it's not worth
+ // the unsafe block, but it would be valid to use `add` instead,
+ // as it cannot overflow.
+ assert_eq!(
+ &foo.a as *const _ as usize,
+ as_bytes.wrapping_add(a_offset) as usize
+ );
+ assert_eq!(
+ &foo.b as *const _ as usize,
+ as_bytes.wrapping_add(b_offset) as usize
+ );
+ assert_eq!(
+ &foo.c as *const _ as usize,
+ as_bytes.wrapping_add(c_offset) as usize
+ );
+}