#question about library conventions and std.json quirks

1 messages · Page 1 of 1 (latest)

spare root
#

I’m working on a basic gql client for zig but these questions really apply to any sort library design that uses std.json as an impl detail

GitHub

a basic GraphQL client for zig. Contribute to softprops/zig-graphql development by creating an account on GitHub.

rigid saffron
#

I mean, you shouldn't be getting segfaults at all, so if you are then yes, there's a better way to do things.

#

You can't deinit parsed and then return the value. I agree with you though, I hate std.json.Parsed(T). I don't see why it's in std.json, just call it std.Owner(T) or something. I realize that's superficial, but at least then it's leaking that value is owned by Owner, instead of being something related to JSON.

#

If you use the default, alloc_if_needed, and you're using parseFromSlice, then the lifetime of the returned data will be tied to the input (for strings at least).

#

While the AllocWhen does make things more complicated, it's also nice control to the caller. There's no way for Zig to know what the lifetime of the input slice is. It could safely always use alloc_always, but that would result in extra allocations for the relatively common case that the input and the parsed json have the same lifetime.

spare root
#

Agree on the Owned type thing. One idea I had was to wrap everything in my own type that when deinit is called I could deinit the json parsed type. That feels like it would be so common to so many cases that I’m surprised this std lib generalization doesn’t already exist. This also makes me miss rusts Drop trait a bit