There doesn't seem to be a way to initialize a value via a pointer to its bytes without first having to "normally" initialize that value, ie:
// var x: T // <-- This will not work, but is what I want, ideally I'd even want it to be `let x: T`.
var x: T = someValueT // <-- I have to unnecessarily initialize `a` before being able to initialize its bytes via a pointer.
withUnsafeMutableBytes(of: &x) {
... `a` will be initialized here ...
}
Note also that apart from the fact that someValueT will be immediately overwritten, there might not even be a someValueT available at this place in the code, ie what if I only have an UnsafeRawBufferPointer to the bytes of a T here?
What I'm looking for is something analogous to the existing
For trivial types the compiler will also optimise this away for you. For example, here is a Godbolt link showing a double-initialization being optimised away.
This optimisation will fail if the compiler cannot prove that the original initialization has no side effects. This means that if there are calls to non-inlinable functions defined outside the module (such as some of the backing functions on Array) the optimisation can fail. For trivial types, though (the only kind of type safe to initialize by bitwise copying) the optimisation should never fail if the type was defined in your module.
The poblem with load is (if I remember correctly) that it requires the memory to be properly aligned for the target type. It has been suggested to provide an unaligned version as well, e.g. here: Built-in unaligned loads.