ChanServ changed the topic of #zig to: zig programming language | https://ziglang.org | be excellent to each other | channel logs: https://irclog.whitequark.org/zig/
msingle has quit [Ping timeout: 246 seconds]
a_chou has quit [Quit: a_chou]
sawzall has quit [Ping timeout: 240 seconds]
sawzall has joined #zig
_Vi has quit [Ping timeout: 244 seconds]
klltkr has joined #zig
_whitelogger has joined #zig
<Cadey> andrewrk: is it normal for the zig stdlib to assume /dev is mounted sanely?
nephele_ has joined #zig
nephele has quit [Ping timeout: 256 seconds]
nephele_ is now known as nephele
ur5us_ has joined #zig
_whitelogger has joined #zig
xd1le has quit [Read error: Connection reset by peer]
xd1le has joined #zig
ur5us_ has quit [Ping timeout: 260 seconds]
<klltkr> Cadey, what's your issue?
cole-h has joined #zig
_whitelogger has joined #zig
klltkr has quit [Ping timeout: 260 seconds]
waleee-cl has quit [Quit: Connection closed for inactivity]
marnix has joined #zig
LakeByTheWoods has joined #zig
LakeByTheWoods has quit [Remote host closed the connection]
cole-h has quit [Quit: Goodbye]
traviss_ has joined #zig
traviss__ has quit [Ping timeout: 240 seconds]
dec05eba has joined #zig
<dec05eba> i rewrote mem.indexOf and mem.lastIndexOf to use boyer-moore-horspool algorithm instead of naive search and it looks like the boyer-moore-horspool version was slower. I guess the compiler is knows the naive version is a string search and optimizes it better
<dec05eba> lol
dec05eba has quit [Quit: Leaving]
brotein has joined #zig
leeward has quit [Ping timeout: 246 seconds]
brotein has quit [Ping timeout: 258 seconds]
xd1le has quit [Ping timeout: 260 seconds]
xd1le has joined #zig
wootehfoot has joined #zig
_whitelogger has joined #zig
metabulation has joined #zig
wootehfoot has quit [Ping timeout: 256 seconds]
metabulation has quit [Ping timeout: 240 seconds]
<KoljaKube> ikskuh: Your suggestion with the duplicated struct definitions works btw. Luckily there are only four cases of it, so I can stomach the duplication
dec05eba has joined #zig
xackus has joined #zig
Notimik has joined #zig
waleee-cl has joined #zig
klltkr has joined #zig
marnix has quit [Ping timeout: 256 seconds]
marnix has joined #zig
klltkr has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
klltkr has joined #zig
klltkr has quit [Client Quit]
klltkr has joined #zig
marnix has quit [Read error: Connection reset by peer]
marnix has joined #zig
dec05eba has quit [Quit: Leaving]
<mkchan> is there a way to mark a function as comptime-only (consteval from c++)? I'm writing a function (in an enum namespace) that returns a pre-built list so i can iterate over it. i suppose inline is equivalent and calling it with comptime at the call-site should be the same as well but that's kind of roundabout
<ikskuh> mkchan: you cannot mark a function comptime-only, but you can just write code that only works at comptime
<ikskuh> the compier will error as soon as you try to call it at runtime
<mkchan> what if i say the return type is `comptime [num]Type`
<ikskuh> just say the return type is [N]Type
<ikskuh> and you'll be happy :)
<fengb> comptime is an expression modifier. Since types are always comptime known, that does nothing
<mkchan> i understand that the compiler will probably do it without actually calling the function, but isn't there a way to be compile-time sure of it?
<mkchan> comptime { return list }; unreachable; seems kinda hacky if it even works lol
<ikskuh> why would you want to call the function only at comptime?
<mkchan> because it's a prebuilt list of enum values, it's obviously comptime-available
cr1901_modern has quit [Ping timeout: 240 seconds]
<mkchan> inline is equivalent for my use case so i don't mind using that if there's really no way to do it comptime
* ikskuh is confused
<mkchan> i don't actually need it at compile time
<ikskuh> if you want to enforce comptime eval, just call the function with `comptime foo()`
<ikskuh> functions in zig will always be agnostic to comptime as long as possible
<mkchan> i mentioned that... it's just that i was hoping to enforce that a function can only be called with comptime and therefore not require the comptime keyword at call-site because that's way more repetitive
<ikskuh> make all args comptime then?
<mkchan> there are no args, it's a no-arg function that has 1 line which returns a list
dec05eba has joined #zig
<KoljaKube> Why isn't it a variable then?
<ikskuh> ^-
cr1901_modern has joined #zig
<fengb> You can do something like `const foo = blk: { do stuff; break :blk result; }`
<mkchan> that works fine for me actually i just didn't try a pub const in enum, thanks
<fengb> Oh yeah enums are containers like structs
<mkchan> in fact that solves my use case, i can just store a comptime-called function into a variable to emulate consteval
<KoljaKube> Is there something in std that lets me convert a [*c]u8 of known length to a []u8?
<ikskuh> std.mem.span
<fengb> If you know the length, you can slice it
<ikskuh> oh yeah, sorry
<KoljaKube> ikskuh: That would count the length, and I don't think the pointer is zero-terminated
<KoljaKube> So ptr[0..known_length]?
<ikskuh> yep
<KoljaKube> Thanks
<KoljaKube> No idea why I blanked on that :D
<KoljaKube> mkchan: There's really no use in forcing comptime if you are working with types. Those are only available at comptime, there is no way your initializer function could be called at runtime
<mkchan> i'm working with values
<KoljaKube> Ah, sorry, misread
<mkchan> enum Stuff { A, B, fn all() Stuff { return [_]{.A, .B}; } } essentially
<mkchan> now i just have a const called all
<ikskuh> @mkchan: that function can be generated with comptime code as well
<mkchan> i figure yeah with some reflection but my actual enum is non-exhaustive so i guess that's a bit more complicated than just iterating through everything?
<mkchan> actually i'd love it if you could point me to the right functions for iterating over the enum to generate that anyway
<mkchan> for exhaustive ones at least
<fengb> std.meta.fields
<mkchan> :') undocumented but let me take a look
<fengb> std.meta is a treasure trove
<mkchan> hm it's actually a really simple function
<ikskuh> <fengb> std.meta is a treasure trove
<ikskuh> true
<ikskuh> <mkchan> hm it's actually a really simple function
<ikskuh> that's true for most zig metaprogamming
<ikskuh> except … some things
<mkchan> i tried at one point to store a generic Writer() and Reader() which i intended to be an abstraction over std.fs.File.Writer and over a socket writer but i couldn't do it
<fengb> What, like your tuple hack? :P
<mkchan> i think i just tried to make a var writer: std.io.Writer in a struct and obviously that doesn't work because it requires comptime params
<mkchan> so then i tried to make a function which takes that at comptime and pass it to the Writer and failed hard
<ikskuh> fengb: interface.zig :D
<fengb> Oh I just realized @Type(.Struct) lets me define tuples in possibly a less dumb way
<ikskuh> yep
<ikskuh> we got that now :)
klltkr has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
<mkchan> is there a way to make a file be implicitly an enum instead of a struct?
<fengb> Nope
<mkchan> damn! i can't shove an anonymous enum into a namespace-like file either right?
<mkchan> it's really just to avoid an extra qualifier name
<ikskuh> huh?
<ikskuh> you can just do const Foo = @import("foo.zig").Foo;
_Vi has joined #zig
<mkchan> that's what i am doing now @import("enums.zig").MyEnum
<mkchan> lastly, does using // zig fmt: off and // zig fmt: on work? it isn't working in my vscode with ZLS for VSCode
<ikskuh> yes it works
<ikskuh> but only for top-level declarations
<mkchan> ouch.. you mean i'd have to surround my entire enum decl with it not just the enum values (that are in a pattern)
<mkchan> that works alright
<fengb> Embrace the formatter
<mkchan> not ideal but thanks for all the enum hackery help
<blueberrypie> Does anyone know if there is a plan (or already existing support) for code coverage in Zig?
<blueberrypie> Currently, I think the way it could be implemented is by parsing the ast and adding that code coverage callbacks to a special build but that seems pretty poor especially with comptime being a thing.
klltkr has joined #zig
gpanders has quit [Ping timeout: 264 seconds]
gpanders has joined #zig
_whitelogger has joined #zig
cr1901_modern has quit [Ping timeout: 256 seconds]
xd1le has quit [Read error: Connection reset by peer]
xd1le has joined #zig
klltkr has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
marnix has quit [Ping timeout: 256 seconds]
xd1le has quit [Read error: Connection reset by peer]
marnix has joined #zig
xd1le has joined #zig
marnix has quit [Read error: Connection reset by peer]
marnix has joined #zig
<mkchan> any chance for a do-while loop?
<ikskuh> nope
<ikskuh> .D
<ikskuh> andrew even wants to remove the for loop, but the community stands strong :D
<mkchan> motivated by trying to keep the language simple eh? I can get behind that coming from c++
<ikskuh> hehe
<shakesoda> i kind of also don't like the for loop, and while's funky glued on statement for incrementing stuff
<mkchan> if with initializers is just about my most hated "normal" syntax (normal in quotes because of the weird stuff the grammar actually allows but no one sane ever uses)
<ikskuh> mkchan: in c++? i love if with init :D
<ikskuh> for loop is okay, makes intention more clear than a construct iterating with some variables
<mkchan> just wrap the if inside another scope
<ikskuh> iteration is just a too common pattern
cr1901_modern has joined #zig
cole-h has joined #zig
<cr1901_modern> I am still learning, it seems... https://github.com/ziglang/zig/issues/6256#issuecomment-687643492
st4ll1 has quit [Read error: Connection reset by peer]
brotein has joined #zig
st4ll1 has joined #zig
dddddd has quit [Ping timeout: 240 seconds]
brotein has quit [Ping timeout: 246 seconds]
klltkr has joined #zig
klltkr has quit [Client Quit]
dec05eba has quit [Quit: Leaving]
cole-h has quit [Quit: Goodbye]
kristoff_it has joined #zig
dddddd has joined #zig
<kristoff_it> Zig SHOWTIME starts in about 40m, in the meantime the preshow is live https://twitch.tv/kristoff_it
marnix has quit [Ping timeout: 260 seconds]
klltkr has joined #zig
SimonNa has quit [Remote host closed the connection]
SimonNa has joined #zig
klltkr has quit [Quit: Textual IRC Client: www.textualapp.com]
smolck has joined #zig
smolck has quit [Quit: leaving]
smolck has joined #zig
<smolck> hello! I'm new to Zig, and I've been looking around trying to figure out how to spawn a process with `std.ChildProcess` and then read from that process's stdout. How can I do that?
<smolck> I've tried to use the spawn function and then access the stdout, but child.stdout is always null
<ikskuh> you can create a new process
<ikskuh> and then set the stdout behaviour for it
<ikskuh> var proc = ChildProcess.init(…)
<ikskuh> proc.stdout_behavior = .Pipe;
<smolck> yeah I tried that, but after doing an init with {"nvim", "--embed"} as args and spawning the process, the stdout is still null
<ikskuh> hm
<smolck> wait a second
<smolck> okay well I have no idea why that didn't work before
<smolck> thank you
<ikskuh> \o/
<ikskuh> you're welcome? :D
<smolck> XD
<smolck> now I guess I can ask how do I read from stdout?
<ikskuh> stdout is a file
<ikskuh> use stdout.?.read()
<ikskuh> or stdout.reader().…()
<ikskuh> see std.io.Reader() to see all functions for that
<smolck> great thank you
brotein has joined #zig
<smolck> is there a way to read all of the bytes out of a File/reader?
<smolck> rather than doing, say, just enough to fill a [100]u8
<smolck> doing var buf: []u8 = undefined; readAll(buf); doesn't work
brotein has quit [Ping timeout: 246 seconds]
<ikskuh> you need to allocate memory
<ikskuh> there's a readAllAlloc :)
<smolck> aha, great that works
<smolck> cool
<smolck> do I have to free the memory from readAllAlloc later?
<Nypsie[m]> Yes
<smolck> how do I do that? depend on the allocator?
<smolck> *does it depend on the allocator
<ikskuh> usually just use allocator.free(result) where allocator is the thing you passed into your alloc function
<Nypsie[m]> `const data = ...readAllAlloc(allocator, ..); allocator.free(data)`
<smolck> cool, just found that function right after I asked the question haha
<smolck> thank you
<cr1901_modern> How does one install a zig library? https://github.com/tiehuis/zig-regex
<cr1901_modern> I'm trying "zig build install" to test the default install location, but it seems because the tests fail (upstream bug) I can't install it
<ikskuh> cr1901_modern: download the library
<ikskuh> then use zig build-exe --pkg-begin my-library-name /some/path/to/lib.zig --pkg-end my-main.zig
<ikskuh> and use @import("my-library-name")
<ikskuh> or you can use the build system and do
<ikskuh> exe.addPackagePath("my-library-name", "/some/path/to/lib.zig");
ur5us_ has joined #zig
<cr1901_modern> cool, I'll do the latter, thanks!
<ikskuh> (look up the CLI args though, this is all from memory)
<cr1901_modern> ikskuh: Can I disable memory leak detection (it's noise to me, and it seems to be coming from the regex library)?
<cr1901_modern> from within build.zig*
<ikskuh> uurm
<ikskuh> you enabled memory leak detection :D
<ikskuh> probably
<cr1901_modern> b.standardReleaseOptions()
<ikskuh> that's not changing anything regarding memory leak detection
<ikskuh> it's your allocators in your code
<cr1901_modern> GeneralPurposeAllocator
<ikskuh> it's using leak detection by default i think
<ikskuh> you can configure it
<ikskuh> but: fix your memory leaks, don't hide them
<cr1901_modern> I have no idea where it's coming from!
<ikskuh> somewhere you're psasing an allocator in
<ikskuh> probably a missing deinit/free
<cr1901_modern> http://ix.io/2wfp
<cr1901_modern> http://ix.io/2wfr
<cr1901_modern> err ignore the ambiguous redirect line lol :P
<ikskuh> yep, the lib is broken
<ikskuh> the arraylist doesn#t get deinited
<cr1901_modern> Adding deinit does not fix it, so I guess the fix is more involved
<cr1901_modern> Err, adding "re.slots.deinit();"*
smolck has quit [Ping timeout: 260 seconds]
smolck has joined #zig
<smolck> how do I pass a C function a char**?
<smolck> I tried var data: [][]u8 = undefined; and then @ptrCast([*c][*c], data) but I'm not sure that's correct
<ikskuh> you need a [*][*]u8 or [*][*c]u8 or [*]*u8, ...
<ikskuh> or use a slice and access .ptr:
<ikskuh> var slice: [][*]u8 = …;
<ikskuh> slice.ptr
<smolck> huh I'm still getting this error: "error: expected type '[*c][*c]u8', found '[*][*]u8'"
<ikskuh> then you need a [*][*c]u8
<ikskuh> but you can ptr-cast those safely afaik
<smolck> hmm, now I'm getting an error related to what I'm trying to do, so I guess I'll go ahead and explain that. I'm trying to do this basically (just slightly different for what I'm doing, but same idea), just in Zig: https://github.com/ludocode/mpack#the-write-api
<smolck> and now it's saying the target data is null
<smolck> for the mpack_writer_init_growable call
<smolck> which I guess makes sense since I'm using var data: [][*c]u8 = undefined;
<smolck> but I thought that was equivalent to: char* data;
<ikskuh> no, that's "char** data";
<ikskuh> without init
<ikskuh> that's the problem with c pointers:
<ikskuh> you never know if it's pointer to one or many elements
<ikskuh> that's just var data: [*c]u8 = undefined; mpack_writer_init_growable(…, &data, …); then
<smolck> thank you, that makes sense
<smolck> so, the docs say to avoid c pointers wherever possible, so is this one of those cases where they should be avoided or . . . ?
<smolck> and also, do I have to free that memory somehow? free(data) is called in the C code, so I'm guessing a similar thing should be called in Zig?
<ikskuh> i would probably do this:
<ikskuh> var ptr: [*]u8 = undefined;
<ikskuh> mpack_writer_init_growable(…, @ptrCast([*][*c]u8, &ptr), …)
<ikskuh> <smolck> and also, do I have to free that memory somehow
<ikskuh> yes, you have to use c free: std.c.free(ptr);
<smolck> huh, if I do defer std.c.free(ptr) it gives this error: "cast causes pointer to be null"
<smolck> once that's called at the end of the scope
<smolck> it appears to work with c.free(ptr) though with c being the @cImport I'm using
<smolck> now, given a char* and the size of what it points to, how can I use that string from zig?
<smolck> well, I guess a better way to say that is given a [*]u8 and usize
kristoff_it has quit [Ping timeout: 264 seconds]
<ikskuh> slice it! ptr[0..size]
<smolck> yes, thank you!
<smolck> probably wouldn't have gotten this far without your help :D
<ikskuh> you're welcome :)
<smolck> (I also wouldn't have set up IRC :P)
<smolck> (if I hadn't had issues with Zig, that is)
<ikskuh> stay a while and listen!
<smolck> okay!
ur5us_ has quit [Ping timeout: 244 seconds]
<smolck> I'm curious, what is the difference between [*]u8 and *[]u8 (or similar)?
<smolck> ah thank you