the word “isbits” implies pointer-freeness only, i.e. copyable with “memcpy” like POD in c++
There is a subtle difference in the way Julia thinks about POD.
For a mutable struct, copying the value vs passing a pointer to the value can affect the result of the program, because mutating the old copy of the value will not affect the new copy of the value.
In C++, the programmer decides whether to use a value or a pointer. In Julia the compiler decides with no control from the user. If the compiler decided to treat a mutable struct as a value and copy it around, that might change the output of the program. So in Julia only immutable types can be treated that way.
I’m developing a Julia program to edit some binary files in pure Julia without external C program.
Julia does have pointers, so you can read isbits types from a buffer on to the stack:
julia> struct T
a::Int64
b::Float64
end
julia> [(f, fieldoffset(T, i)) for (i, f) in enumerate(fieldnames(T))]
2-element Array{Tuple{Symbol,UInt64},1}:
(:a, 0x0000000000000000)
(:b, 0x0000000000000008)
julia> bytes = Vector{UInt8}(sizeof(T) * 2);
julia> p = pointer(bytes);
julia> unsafe_store!(convert(Ptr{T}, p), T(42, 42.0));
julia> unsafe_store!(convert(Ptr{T}, p), T(7, 7.0), 2);
julia> unsafe_load(convert(Ptr{T}, p))
T(42, 42.0)
julia> unsafe_load(convert(Ptr{T}, p), 2)
T(7, 7.0)
julia> string(bytes)
"UInt8[0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x40, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x40]"
You can sprinkle some metaprogramming on top to make it nicer, if you’re doing a lot of it. We have an internal library for working with on-disk data-structures that we should open-source at some point.