hun-offsetof 0.1.1

Provides C-like macros: offset_of and container_of
Documentation
  • Coverage
  • 25%
    1 out of 4 items documented1 out of 4 items with examples
  • Size
  • Source code size: 18.98 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 294.22 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 4s Average build duration of successful builds.
  • all releases: 4s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • h1467792822

hun_offsetof

Provides C-like macros: offset_of and container_of

和memoffset crate的区别- Differences between crate memoffset and crate self

  1. 基于指针操作,不占用栈空间,支持超大数据结构,无栈溢出风险
  2. Pointer-based opertions do not occupy stack space, support super-large data structures, and avoid stack overflow risks.
  3. 提供container_of操作
  4. Provides the container_of operation.

基于指针的操作属于unsafe范围,如果使用声明宏方式,可能出现嵌套unsafe的编译告 警,而过程宏可以消除这类告警。 Pointer-based operations are unsafe, If the declaration macro mode is used, nested unsafe compilation warnings may be generated, which can be eliminated by procedure macro.

interfaces

offset_of!(type, member) -> usize;
container_of!(&obj, type, member) -> &type;
container_of_mut!(&obj, type, member) -> &mut type;

Examples


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 _);