Currently writing a gemtext and scrolltext parser in Zig. It's not so bad so far, although dealing with strings takes some getting used to.

Posted in: s/Zig

๐Ÿš€ clseibold

Mar 19 ยท 4 months ago ยท ๐Ÿ‘ lanterm, ps ยท ๐Ÿค” 1

24 Comments โ†“

๐Ÿ€ meidam [mod] ยท Mar 19 at 10:56:

I actually do some gemtext parsing in my CLI client for spartan, that I'm writing in zig to become better at the language. And yeah, I feel ya about the strings

๐Ÿš€ clseibold [OP] ยท Mar 19 at 11:40:

@meidam Ah, we're doing the same thing at the same time then, lol.

The strings aren't the worst thing in the world having come from both Golang and Odin, but I feel the standard library is missing a few things that could make strings a little easier to deal with, like a strings builder. That and I kinda hate const with a passion right now, and string literals use that, lmao.

๐Ÿ€ meidam [mod] ยท Mar 19 at 11:55:

Hehe, I actually did it like a year ago and it's been on hold since then, but I'm about to continue working on the client. I also plan on making it a TUI client in the future using libvaxis for it. But we'll see what happens @clseibold.

Are you planning on using your parser written in for profectus? Or are you planning on releasing it as a zig library? Or maybe you're just playing around with zig at the moment?

๐Ÿš€ clseibold [OP] ยท Mar 19 at 12:00:

@meidam I started it mainly to figure out Zig, but also to work on my Scroll protocol, because I like to design the protocol alongside an actual application. I was actually planning on using libvaxis to create a TUI app, since I just learned about libvaxis like 2 hours ago :D

Since Zig seems to have a TLS library, I am planning on moving profectus over to it at some point in the future. I'm not sure when yet. Golang is really not suited for GUI applications, lol.

But for now, it'll just be a library and a TUI client.

Sidenote: I have a Rust gemini parser that I never released, but it's what I'm basing my zig parser off of, and the conversion to zig is not all that different, which is cool. I'm just using tagged unions in zig instead of Rust's fancy enum stuff.

๐Ÿ€ meidam [mod] ยท Mar 19 at 12:38:

@clseibold When I started making the client I couldn't figure out how to use self signed certificates with zig's standard library, so I figured I'd just make it a spartan client first. I'm planning on adding gemini to it later, when I have a good client for the spartan protocol. It works fine as is, if you just want to browse text and gmi files, but there's still a bunch of stuff that I'd like to add before I think it's worth actually using.

I do fantasize about it being the next amfora though xD At least I'm kinda hoping that it can replace amfora for my personal usage at some point. I just need to get myself going somehow.

๐Ÿ‘ป ps ยท Mar 19 at 12:45:

I'm currently stuck depressed and angry a bit with my own libraries implementation for Gemini:

as learning Rust about 1 year, everything looks not optimal for me today.

Today, I want make zero-copy implementation, and delegate headers feature to the separated low-level std crates to reuse it as the component for middle level crates.

To be short: if you are thinking about official library, think about make its components atomic:




Just for a tip, my thoughts for today.

๐Ÿš€ clseibold [OP] ยท Mar 19 at 12:53:

@ps Yeah, writing my own Rust code for a gemtext parser was really obnoxious. I seriously hate Rust so much, lol. My Rust gemtext parser doesn't do any allocation for the parsing, it only does allocation when you want to print out what has been parsed. I frequently ran into ownership/borrowing problems because of this.

Additionally, in Rust there's ownership problems when you try to take two separate slices of one string. Pretty sure it took literally days to figure that crap out, lol.

๐Ÿš€ clseibold [OP] ยท Mar 19 at 12:54:

@meidam I look forward to seeing it!

๐Ÿ‘ป ps ยท Mar 19 at 13:28:

@clseibold that's because beginners like me prefer to create owned data types, and only after that thinking about zero copying, when sometimes it's too late :)

I like Rust for modern ideas like read-only vars, not-null data types, etc. Ownership is not an issue with lifetimes, but it requires real experience and understanding from project begin how exactly should it be implemented, that happened rarely on practice. In most of cases I never know which of features really want to have.

The most problematical for me yet is merge classic clang paradigm (Glib) with traits implementation. That requires lot of abstractions. But there is no useful native GUI library yet, like DB also.

Rust is hard game.

๐Ÿš€ clseibold [OP] ยท Mar 19 at 13:54:

@ps Eh, Rust's ownership and borrowing only requires experience in *Rust*. Programming languages outside of rust work perfectly fine without Rust's crap.

Sure, Rust's compiler can catch some bugs with dangling pointers, use after frees, and a few other things. But many other programming languages have ways to take care of these, and all modern languages (zig, golang, odin, and Rust, among others) take care of some of the most common bugs, which are *out of bounds* issues (and undefined behavior). Switching from C to Zig already takes care of *a lot* of safety issues.

Rust's safety features are somewhat overstated, and also impose very large downsides, like making *common sense* things harder to do, and very slow compilation speeds.

Improving Rust's lifetime inferencing would go a long way to improving the language overall, but many Rust devs seem to be very resistant to expanding it to the lengths that I think the language needs.

The primary issue I ran into with my parser was not dealing with my own ownership and borrowing issues, but dealing with how the standard library handles ownership and borrowing, lol. There is literally one particular function in the standard library that splits a string into lines and iterates on the lines that passes in an unexpected type into the lambda that you give it, and it's completely inconsistent with other parts of the standard library, and it completely messed up the ownership and borrowing crap in the program.

๐Ÿ‘ป ps ยท Mar 19 at 14:04:

I've tried C and can only conclude that I'm an idiot who makes fatal errors in applications larger than a few lines. I make mistakes and write trash code even in Rust, but at least it doesn't contain memory leaks.

๐Ÿš€ clseibold [OP] ยท Mar 19 at 14:07:

@ps The standard library in Rust has a particular map function on a lines iterator, and you give this map function a lambda. All seems fine... until you find out that Rust is passing the *ownership* of the line into your freaking lambda instead of a borrowed string slice! Makes no damn sense.

๐Ÿš€ clseibold [OP] ยท Mar 19 at 14:09:

@ps Idk, memory leaks are mostly easy to avoid, imo. The best trick that you should learn, however, is how to allocate memory in bulk, something that Rust doesn't prioritize like... *at all*, which is really a shame because this not only speeds up memory allocations, it helps a little bit with the memory management by requiring less to manage - less allocations, therefore less frees.

๐Ÿ‘ป ps ยท Mar 19 at 14:16:

All seems fine... until you find out that Rust is passing the *ownership* of the line into your freaking lambda instead of a borrowed string slice! Makes no damn sense.

Yes, I know about this case. I also found stranger things, like how the compiler allows decrements of the usize type to go below 0. There is a community forum for developers, but I feel too young for that.

๐Ÿ€ meidam [mod] ยท Mar 19 at 14:21:

Stranger Things mentioned

๐Ÿ‘ป ps ยท Mar 19 at 14:31:

@clseibold the issue with map reference could be just misunderstanding of

โ€” Place Expressions and Value Expressions

I had same problem before, because expected another logic from borrowing that actually is. It's not always clear. To understand this language well, the user should be familiar with rustc backend, imho. It is like JavaScript when you sometimes don't know what to expect until get learn the backend logic for specified version.

In 99% of cases, developers only think that they're understand something :)

๐Ÿ‘ป ps ยท Mar 19 at 14:32:

Stranger Things mentioned

@meidam yah! ๐Ÿ˜‰

๐Ÿš€ clseibold [OP] ยท Mar 19 at 14:38:

@ps I don't know why you keep thinking I "don't understand things", but I don't see how the thing I mentioned has anything to do with place expressions and value expressions.

Assuming that every critique of Rust is because someone "doesn't understand" the language is beyond ridiculous, illogical, sophistic, presumptuous, and extremely insulting, especially when you've insulted literally every non-Rust developer, which is what you did when you suggested that people who don't understand Rust don't know how to think about the design of their programs correctly.

There is a map function on a lines iterator that give you a str, not a string slice (&str). The types are very clear. I'm not a f*cking idiot who can't read the types that need to be passed into functions.

Because of the type of the lambda that needs to be passed into this map *function*, you literally cannot slice the string and return those slices, because that would be returning a borrow where the ownership goes out of scope *and* you cannot pass back the ownership along with any borrows in returns to functions, so the only thing you can do is return new strings (which require allocation). This is a *real* problem in the standard library, and it is also inconsistent with other map functions that *do* pass in a string slice (&str) rather than a string.

The link you cited has literally *nothing* to do with what I'm talking about. But if it helps you feel better about Rust to pretend like I'm some idiot who doesn't know what he's talking about, then so be it, but I then do not need to engage in such unserious and bad faith conversations.

๐Ÿ‘ป ps ยท Mar 19 at 17:34:

@clseibold sorry, maybe it's my English - I did not meant you don't understand something. I'm talking about myself - just when I think that understand something - it is always not. Now I want only to left social places and go back to my silence.

๐Ÿš€ lanterm ยท Mar 19 at 22:47:

I've been writing Zig for a good while now, and I definitely felt the same way about strings for a while, but I have to say, I have come to love std.ArrayList(u8) as a string builder. If you're not aware already, the zig compiler ships with a command `zig std` that will pop open std lib documentation in a browser, all on your local machine.

When I got started, I found "the missing zig guide" ziglearn.org very valuable as a reference. I think they've rebranded, I'll share their url here:

โ€” https://zig.guide

๐Ÿš€ stack ยท Mar 20 at 01:28:

@ps, don't feel bad or apologize. Some people have odd rage issues, as you can see.

๐Ÿš€ clseibold [OP] ยท Mar 20 at 04:27:

@ps I apologize. I'm not upset with you talking about your own experiences, but you said "In 99% of cases, developers only think that they're understand something :)" which goes far beyond your own experiences and makes an assertion about all developers as a whole.

It is frustrating when someone talks about something they don't like in Rust and Rust people tend to respond defensively with "well, it's probably because you don't understand the language" or "well, you're probably doing wrong things". That's how I read your posts, so if you didn't intend that, then I apologize.

This type of response I mention was extremely common when Rust started becoming mainstream around 7 years ago. The Rust community was notorious for evangelization and hating on and insulting people who don't like and who critique Rust.

I don't even think Rust is terrible, and I was so excited for it back in 2015 and 2016. And back then it was much more simple than it is now. I used it a little back then, but wasn't very interested in using it for any main projects.

I've now come back to it years later, and some things have simplified, and some things are way more complicated. Back in 2015 you didn't even have lifetime inferencing, lol.

I also find how most people write Rust code to be very convoluted, in the same way I find how most people write Python code to be convoluted. Python is worse, I suppose, with all of the classes and layers of indirection between classes, and the __weird_variables__ all over the place.

Rust certainly helps a lot, but it is important to point out that it is not the *ONLY* way to write safe code!! Golang, C#, and Java all have a GC that make them safer than C/C++ too! Haskell has had some of the features Rust took for years! So I absolutely *cannot* stand some Rust "evangelizers" that act like Rust is the one true language. Programming languages should not be religions, they should be tools.

๐Ÿš€ clseibold [OP] ยท Mar 20 at 04:36:

@lanterm Thanks for the link! I was thinking one could use ArrayList, but I guess while it works quite well, it would be better to have simple functions/methods for appending and popping off a utf-8 character, for example, and things like that. You could use it with the unicode module (or whatever zig calls them), and with zg/ziglyph I guess.

I'll check out that zig.guide link. Thanks again.

๐Ÿ‘ป ps ยท Mar 20 at 14:23:

@clseibold no problem for me, glad that we finally understand each other

but you said "In 99% of cases

that's not 100% haha! :D

Anyway, I plan to learn new language in the next life, where probably it will be interaction with the AI using telepathy.


Source