Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/azurefx/cunion.jl
C-like unions for Julia.
https://github.com/azurefx/cunion.jl
julia union
Last synced: about 1 month ago
JSON representation
C-like unions for Julia.
- Host: GitHub
- URL: https://github.com/azurefx/cunion.jl
- Owner: azurefx
- License: mit
- Created: 2019-12-05T10:11:24.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2019-12-11T03:18:52.000Z (almost 5 years ago)
- Last Synced: 2024-10-11T14:41:22.856Z (about 1 month ago)
- Topics: julia, union
- Language: Julia
- Homepage:
- Size: 7.81 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CUnion.jl
[![Build Status](https://travis-ci.org/azurefx/CUnion.jl.svg?branch=master)](https://travis-ci.org/azurefx/CUnion.jl)
This package provides C-style primitive union types for Julia.
## Usage
Add the macro `@union` to a `struct` definition to make a C union:
```julia
@union struct U
x::UInt8
y::UInt16
end
```Then `U` can be instantiated with one of its field types:
```julia
julia> u = U(Int8(-1))
U1(0x00ff)julia> u1.y
0x00ffjulia> reinterpret(Int16, u)
255
```Nested anonymous structs are also supported. For example, the `LARGE_INTEGER` type from Win32
```cpp
typedef union _LARGE_INTEGER {
struct {
DWORD LowPart;
LONG HighPart;
} DUMMYSTRUCTNAME;
struct {
DWORD LowPart;
LONG HighPart;
} u;
LONGLONG QuadPart;
} LARGE_INTEGER;
```
Can be written as
```julia
@union struct LargeInteger
struct u
low::UInt32
high::UInt32
end
quad::UInt64
end
```
```julia
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?