<kandinski>
so maybe the solution is to have an ._errorbuffer field and a var errormessage declaration with the slice
<kandinski>
I like this pattern
<jayschwa>
Personally, (without knowing more context), I would have a buffer field and a length field. Then create the slice only at the point where you need it (e.g. const slice = struct_val.buffer[0..struct_val.len]). If the slice is also a field on the struct, and an instance of the struct is copied, the copy's slice pointer will still be to the original
<jayschwa>
struct buffer, not the copy's buffer.
<kandinski>
jayschwa: so a pub struct function is what I need
<kandinski>
foo.errormessage()
<kandinski>
jayschwa: the context is that I'm wrapping pcre2 (for now, just enough to have a nice API for myself, I'm not writing a library for public consumption)
<kandinski>
so I'm doing a lot of passing out buffers to C for things like error messages
<kandinski>
but I want the consuming code to be clean and ziggish
<kandinski>
buffer/length are required because that's what pcre2.get_error_message(&buffer) returns: length as the return value and it writes the bytes in the buffer.
<kandinski>
given this context, would you say implementing it so it can be consumed as `message: []u8 = re_matcher.errormessage()` is the ziggish approach?
tundrax has quit [Remote host closed the connection]
<jayschwa>
Looks reasonable to me
<kandinski>
ta. I'm not only new to Zig but not a C programmer. I'm only learning enough C to understand memory models etc when interfacing. So I appreciate any advice not only about the technology, but about conventions and design.
<kandinski>
(in this case, I also learnt about the dangers of copying structs with slices as members)
nullheroes has joined #zig
a92 has joined #zig
cole-h has quit [Ping timeout: 246 seconds]
a_chou has joined #zig
squeek502 has quit [Remote host closed the connection]
frmdstryr has joined #zig
jj__ has joined #zig
ryuukk_ has joined #zig
jjsullivan1 has quit [Ping timeout: 240 seconds]
earnestly has quit [Ping timeout: 260 seconds]
supercoven_ has joined #zig
supercoven has quit [Ping timeout: 240 seconds]
a_chou has quit [Ping timeout: 260 seconds]
Ondingen has joined #zig
<Ondingen>
I've hit a c-to-zig translation error and I'm trying to make a minimal example. How do I get all the defines set as they are when using @cImport(@cInclude(...))? Right now I'm doing: "zig translate-c -target i386-windows-msvc -Ipath/to/includes path/to/winnt.h" but the target doesn't seem to affect the defines.
a_chou has joined #zig
wilsonk has left #zig [#zig]
<kandinski>
so I'm writing this C wrapper and would like to test whether I'm freeing all memory (using the c heap) in my deinit() function. Can anyone point me to a test that checks for that?
_whitelogger has joined #zig
<ifreund>
valgrind would certainly let you check that, but you can't use it in a test case
<ifreund>
I think your best bet for that is using std.heap.GeneralPurposeAllocator with std.heap.c_allocator as the backing allocator
<ifreund>
the general purpose allocator will check for leaks
<ifreund>
Ondingen: there are no defines set by default. You can set any you like using @cDefine()
<Ondingen>
ifreund That can't be true, there are several defines that aren't set when doing translate-c, like _X86_ etc. Am I misunderstanding something?
<Ondingen>
and when I manually set them, I get complaints about unknown type names for DWORD, BYTE etc.. Do I have to manually add all the dependencies to winnt.h for it to work?
<ifreund>
hmm, maybe you need to pass -lc ?
<Ondingen>
Does not make a difference
<kandinski>
ifreund: I'm not passing an allocator expliclty to pcre2 though. I'm just letting it allocate and then calling its own freeing functions.
<Ondingen>
For the target, i386-windows-msvc, I have to add all the includes manually anyway so that's what I'm doing for translate-c aswell
<janiczek>
so the trick is to only let it infer (with the underscore) the one that I repeat with ** ?
<ifreund>
you want to repeat a length 1 array 50 times, not a length 50 array 50 times
<ifreund>
that make sense?
<janiczek>
I guess so. I was guessing wrong what the [1][6] means at the beginning of the array literal
<janiczek>
ie. I thought it's the length after repeating
<janiczek>
but it's length before repeating, then it's repeated, and the final length is implicit?
<g-w1>
yes
<janiczek>
great, thanks folks!
<g-w1>
const lights: [50][6];
<janiczek>
^ false = default element if I don't initialize explicitly?
<g-w1>
no
<g-w1>
im just telling you the type
<janiczek>
oh right
<janiczek>
I skipped over the : instead of =
siulManfroni has joined #zig
luismanfroni has quit [Read error: Connection reset by peer]
siul_ has joined #zig
janiczek has quit [Remote host closed the connection]
siulManfroni has quit [Ping timeout: 272 seconds]
braket has joined #zig
<justin_smith>
similar question: for an API, I want a struct to hold a slice of (variable) N slices of f64, so I declared "outputs: ?[][]f64", and tried to initialize with ".outputs = &[1][]f64{undefined}" - meaning a slice of 1, not yet initialized with the slice it will hold
<justin_smith>
I get "expected type '[][]f64', found *const [1][]f64"
<ifreund>
that's an issue with constness
<ifreund>
array literals are always const but your fuction expects a mutable slice
<justin_smith>
ahh, so the number is a red herring, OK
<justin_smith>
then I need to initialize this differently, thanks
<ifreund>
no problem
<ifreund>
to give more detail: *const [1][]f64 is a constant pointer to an array of one f64 slice. A pointer to an array may be coerced to a slice because the length of the array is comptime known
<ifreund>
but only if the constness of the array agrees with the constness of the slice
<justin_smith>
that makes sense - the idea is that each struct instance will know its expected number of output slices, but I want a single struct that can hold each
<justin_smith>
err, that can hold any of them
<ifreund>
if the number is comptime known you could make it generic
<justin_smith>
that's a good point, it might be time for me to learn generics
<ifreund>
not much to learn really, they're just functions that take some comptime parameters and return a type
AceNovo has joined #zig
wootehfoot has joined #zig
Ondingen has joined #zig
wilsonk has left #zig [#zig]
Ondingen has quit [Remote host closed the connection]
riba has joined #zig
siulManfroni has joined #zig
siul_ has quit [Read error: Connection reset by peer]
supercoven_ has quit [Ping timeout: 240 seconds]
siulManfroni has quit [Client Quit]
moo has joined #zig
ur5us has joined #zig
moo has quit [Read error: Connection reset by peer]
ryuukk_ has joined #zig
ryuukk__ has quit [Ping timeout: 246 seconds]
wootehfoot has quit [Read error: Connection reset by peer]
donniewest has quit [Read error: Connection reset by peer]
donniewest has joined #zig
osa1 has joined #zig
nyaayaya has quit [Quit: Ping timeout (120 seconds)]
nyaayaya has joined #zig
marnix has quit [Read error: Connection reset by peer]
marnix has joined #zig
Akuli has quit [Quit: Leaving]
marnix has quit [Remote host closed the connection]
marnix has joined #zig
skuzzymiglet has joined #zig
frett27_ has joined #zig
AceNovo has quit [Remote host closed the connection]
frett27 has quit [Ping timeout: 260 seconds]
tane has quit [Quit: Leaving]
riba has quit [Ping timeout: 256 seconds]
osa1 has quit [Remote host closed the connection]
skuzzymiglet has quit [Ping timeout: 260 seconds]
frett27_ has quit [Ping timeout: 246 seconds]
AceNovo has joined #zig
marnix has quit [Ping timeout: 264 seconds]
donniewest has quit [Quit: WeeChat 2.9]
AceNovo has quit [Quit: Konversation terminated!]
AceNovo has joined #zig
AceNovo has quit [Quit: Konversation terminated!]
AceNovo has joined #zig
AceNovo has quit [Quit: Konversation terminated!]
AceNovo has joined #zig
omglasers2 has quit [Read error: Connection reset by peer]