ChanServ changed the topic of #zig to: zig programming language | ziglang.org | be excellent to each other | channel logs: https://irclog.whitequark.org/zig/
noonien has joined #zig
<daurnimator> donpdonp: btw, you can just use `i32(-1)` rather than `@intCast(i32, -1)`.
<daurnimator> the former is fully defined and safety checked; the latter is potentially unsafe.
<daurnimator> So I'm trying to figure out how to translate some complex structure definitions/usage to zig
<daurnimator> and I think there's a few missing language features
<daurnimator> lets start with: usize bytes. If the lowest bit is even, then consider it to be an aligned pointer. otherwise cast the top bit to a boolean.
<daurnimator> Is this the right starting point? union { Ptr: align(32) *c_void, Bool: bool }
<daurnimator> but then how can I tell zig how to pick which bit the boolean is?
<daurnimator> or how can I tell it that the bottom bit is the "tag" bit?
<daurnimator> s/align(32)/align(2)/
redj has quit [Read error: Connection reset by peer]
kristate has joined #zig
kristate has quit [Remote host closed the connection]
kristate has joined #zig
kristate has quit [Remote host closed the connection]
kristate has joined #zig
kristate has quit [Remote host closed the connection]
kristate has joined #zig
redj has joined #zig
kristate has quit [Remote host closed the connection]
kristate has joined #zig
kristate has quit [Remote host closed the connection]
<Marumoto> does linkSystemLibrary look in /usr/lib?
jevinski_ has quit [Read error: Connection reset by peer]
jevinskie has joined #zig
jevinski_ has joined #zig
jevinskie has quit [Ping timeout: 250 seconds]
wilsonk has quit [Read error: No route to host]
hooo has quit [Quit: Connection closed for inactivity]
wilsonk has joined #zig
tbodt has quit [Quit: ZNC 1.6.5+deb2build2 - http://znc.in]
<daurnimator> Marumoto: if I recall correctly, not by default.
tbodt has joined #zig
reductum has joined #zig
darithorn has joined #zig
darithorn has quit [Quit: Leaving]
Marumoto has quit [Ping timeout: 244 seconds]
kristate has joined #zig
fsateler_ has joined #zig
fsateler has quit [Ping timeout: 250 seconds]
_whitelogger has joined #zig
Hejsil has joined #zig
<Hejsil> daurnimator, We don't have a way to specify the location of the union tag, so I don't think a union is the right choice for your problem
<Hejsil> I think a struct with a usize field and methods that does the conversion is the best you can do
<Hejsil> struct {f:usize, fn toBool(me: @This()) ?bool {...} fn toPtr(me: @This()) ?align(32) *c_void {...} }
<Hejsil> This would allow you to write you code like this:
<Hejsil> if (me.toBool()) |b| {} else if (me.toPtr()) |p| {} else {}
<Hejsil> You could also convert it to a union: switch (me.toUnion) {T.Bool => |b| {}, T.Ptr => |p| {}}
<Hejsil> me.toUnion()*
reductum has quit [Quit: WeeChat 2.3]
<daurnimator> Hejsil: hmmm. what about this if we had anonymous unions: union { packed struct { Ptr: align(2) *cvoid, isPtr: bool }, packed struct { Bool: bool, _: u63}, tag(self: *This()) @tagType(*This()) { if (self.isPtr) { return @typeOf(self.Ptr) } else { return @typeOf(self.Bool) } } }
<Hejsil> Well, packed struct { Ptr: align(2) *cvoid, isPtr: bool } wont work, as isPtr is a bit outside the pointers bits. Also, it seems that your tag function tries to return a type a runtime
<Hejsil> packed union { Data: packed struct { b: bool, _: u62, isPtr: bool }, Ptr: *align(2) c_void, } could work to represent your data I think
<Hejsil> What is this even used for. Seems like a fearly complex packing of data
<daurnimator> Hejsil: it's a simplified example of what I'm looking at doing
<daurnimator> Hejsil: one thing I'm looking at is implementing the maple tree in zig. https://www.youtube.com/watch?v=-Bw-HWcrnss&t=935
tiehuis has joined #zig
<tiehuis> Added a zig jpg image writer to this repository if anyone happens to need one: https://github.com/tiehuis/zig-raytrace
tiehuis has quit [Client Quit]
slugspace has joined #zig
<daurnimator> Hejsil: the idea is that *most* pointers are align(4), so you can store them "inline". otherwise they are a pointer to another another area. with a tag.
<daurnimator> one of the more interesting things in a maple tree IMO is the "sparse" format, the value idea is: {x, val1, y, val2, z }. which says that between x and y, the value is val_1, between y and z it is val_2
<daurnimator> and the value is another tree: which can be an inline pointer, a value, or a pointer to another dense or sparse node
<daurnimator> the size of a sparse node {w,val0,x,val1,y,val2,z} is ideally made to be the size of a cache line
<daurnimator> and the whole thing can be made thread-safe!
slugspace has quit [Ping timeout: 240 seconds]
kristate has quit [Read error: Connection reset by peer]
kristate has joined #zig
<Hejsil> daurnimator, i see. One thing we might want is more control over the layout of tagged unions (at least packed ones).
<Hejsil> So: packed union(enum(u1)){ Bool: packed struct { b: bool, _: u62 }, Ptr: u63, __tag: u1 }
<Hejsil> Where __tag is just a made up way of specifying where the tag is in the union
<Hejsil> You would still need to @bitCast your union to the ptr
<Hejsil> Ooh wait, we need usize and not u64
<Hejsil> Hmmm
schme245 has joined #zig
<schme245> hello! I want to take the first 4 bytes of a byte array and convert that to a u32. here's my attempt:
<schme245> var block: [8]u8 = undefined;
<schme245> // ...
<schme245> // some code to fill the block
<schme245> // ...
<schme245> const left: u32 = @bitCast(u32, @truncate(u32, @ptrToInt(block[0..3].ptr)));
<schme245> is this correct? is there a better way to do it?
<euantor> @schme245: check out `std.mem.readIntSlice`: https://github.com/ziglang/zig/blob/master/std/mem.zig#L529
<euantor> That's the easy way to do it :D
<schme245> perfect, thanks! :D
<Hejsil> In case you know that your bytes are of the platforms endianess, you can just use @bytesToSlice(u32, block[0..4])[0]
steveno has joined #zig
schme245 has quit [Remote host closed the connection]
schme245 has joined #zig
schme245 has quit [Remote host closed the connection]
schme245 has joined #zig
halosghost has joined #zig
Hejsil has quit [Ping timeout: 256 seconds]
kristate has quit [Remote host closed the connection]
darithorn has joined #zig
schme245 has quit [Remote host closed the connection]
schme245 has joined #zig
schme245 has quit [Remote host closed the connection]
schme245 has joined #zig
steveno has quit [Ping timeout: 268 seconds]
schme245 has quit [Ping timeout: 240 seconds]
Zaab1t has joined #zig
Avila has joined #zig
steveno has joined #zig
schme245 has joined #zig
schme245 has quit [Remote host closed the connection]
wootehfoot has joined #zig
steveno has quit [Ping timeout: 240 seconds]
MajorLag has quit [Ping timeout: 240 seconds]
MajorLag has joined #zig
MajorLag has quit [Ping timeout: 250 seconds]
<donpdonp> here's another webasm oddity. in zig i can export fn setup() [*]const u8 {} and the function 'setup' will be in the .wasm file, where without the export it wouldnt be 'exported' into the wasm. the problem im running into is I need setup to be an export in the wasm, and its not.
MajorLag has joined #zig
Marumoto has joined #zig
<donpdonp> ah nevermind. zig HEAD fixes it :)
schme245 has joined #zig
Zaab1t has quit [Quit: bye bye friends]
<schme245> how do i bitshift left with a runtime value?
<schme245> comptime const indices = [8]u8{58, 50, 42, 34, 26, 18, 10, 2};
<schme245> for (indices) |i| {
<schme245> x ^ (1 << i);
<schme245> }
<schme245> error: LHS of shift must be an integer type, or RHS must be compile-time known
<schme245> x ^ (1 << i);
Marumoto has quit [Remote host closed the connection]
<schme245> ahh, x ^ (u64(1) << i) does the trick
Avila has quit [Ping timeout: 246 seconds]
darithorn has quit [Quit: Leaving]
<emekankurumeh[m]> i wish zig could get some better type inference, but being explicit about types doesn't really hurt either.
steveno has joined #zig
halosghost has quit [Quit: WeeChat 2.3]
<daurnimator> emekankurumeh[m]: hmm? the only inference that is really missing is return types of functions
<daurnimator> emekankurumeh[m]: and that was a deliberate design choice
<daurnimator> emekankurumeh[m]: is there any other place you'd like it to be better?
schme245 has quit [Remote host closed the connection]
wootehfoot has quit [Read error: Connection reset by peer]
steveno has quit [Ping timeout: 250 seconds]
kristate has joined #zig
schme245 has joined #zig
schme245 has quit [Ping timeout: 240 seconds]