CUnion.jl

C-like unions for Julia.
Author azurefx
Popularity
5 Stars
Updated Last
3 Years Ago
Started In
December 2019

CUnion.jl

Build Status

This package provides C-style primitive union types for Julia.

Usage

Add the macro @union to a struct definition to make a C union:

@union struct U
  x::UInt8
  y::UInt16
end

Then U can be instantiated with one of its field types:

julia> u = U(Int8(-1))
U1(0x00ff)

julia> u1.y
0x00ff

julia> reinterpret(Int16, u)
255

Nested anonymous structs are also supported. For example, the LARGE_INTEGER type from Win32

typedef union _LARGE_INTEGER {
  struct {
    DWORD LowPart;
    LONG HighPart;
  } DUMMYSTRUCTNAME;
  struct {
    DWORD LowPart;
    LONG HighPart;
  } u;
  LONGLONG QuadPart;
} LARGE_INTEGER;

Can be written as

@union struct LargeInteger
  struct u
    low::UInt32
    high::UInt32
  end
  quad::UInt64
end
julia> li=LargeInteger(0x00112233aabbccdd)
LargeInteger(0x00112233aabbccdd)

julia> li.u.high
0x00112233

TODO

  1. Improved reinterpret_cast performance?
  2. Convenient methods to update fields?
  3. Sub-typing and generic support?

Required Packages

Used By Packages

No packages found.