# hioff **Repository Path**: 1467792822/hioff ## Basic Information - **Project Name**: hioff - **Description**: Provides C-like macro: offset_of and container_of for RUST - **Primary Language**: Rust - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2023-01-31 - **Last Updated**: 2025-01-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # `hioff` The original name is `hun_offsetof`. Provides C-like macros: `offset_of` and `container_of` # interfaces ```rust offset_of!(type, member) -> usize; container_of!(&obj, type, member) -> &type; container_of_mut!(&obj, type, member) -> &mut type; ``` # Examples ```rust extern crate hun_offsetof as hun; #[repr(C)] struct Bar { key: i32, value: i32, } #[repr(C)] struct Foo { key: i32, value: [Bar; 2], } assert_eq!(hun::offset_of!(Bar, value), 4); assert_eq!(hun::offset_of!(Foo, value[1].key), 12); let foo = Foo { key: 1, value: [ Bar { key: 2, value: 2}, Bar { key: 3, value: 3 }], }; let value = &foo.value[1].value; let obj = unsafe { hun::container_of!(value, Foo, value[1].value) }; assert_eq!(obj as *const _, &foo as *const _); ```