Skip to main content

Mojo module

unsafe_union

Defines an untagged union type for C FFI interoperability.

This module provides a C-style union type that allows storing different types in the same memory location without runtime type tracking. Unlike Variant, UnsafeUnion does not maintain a discriminant field, making it suitable for:

  • Interfacing with C libraries that use union types
  • Low-level memory manipulation and type punning
  • Situations where memory layout must exactly match C unions

Warning: Using UnsafeUnion is inherently unsafe. Reading a value as the wrong type results in undefined behavior, just like in C. Prefer Variant for safe, type-checked sum types.

Structs

  • UnsafeUnion: An untagged union that can store any one of its element types.

Was this page helpful?