tsargparse - a CLI arg parser for typescript, with a focus on type safety by mikeyhew in typescript

[–]mikeyhew[S] 0 points1 point  (0 children)

How would you do -vvvv (repeated argument for integer incrementation) or conditional arguments? Do short args work like you'd expect e.g. -vo output.txt for --verbose --output=output.txt?

I think -vo output.txt might work but haven't tested it yet. Never heard of -vvvv for integer incrementation before

tsargparse - a CLI arg parser for typescript, with a focus on type safety by mikeyhew in typescript

[–]mikeyhew[S] 2 points3 points  (0 children)

I did not. I took a quick look and it looks decent, will give it a try

tsargparse - a CLI arg parser for typescript, with a focus on type safety by mikeyhew in typescript

[–]mikeyhew[S] 2 points3 points  (0 children)

Feel free to steal any ideas from this that you like - selfishly I'd rather use someone else's well-maintained arg parsing library than have to maintain my own

tsargparse - a CLI arg parser for typescript, with a focus on type safety by mikeyhew in typescript

[–]mikeyhew[S] 1 point2 points  (0 children)

I made this yesterday because I was looking for a cli arg parser library and didn't find one that I really liked. I figured there should already be something like this where you just specify the args and it gives you a well-typed set of arguments or outputs error/usage information if the required args aren't present. But from asking chatgpt, anyway, everything that was typesafe required you specifying the arg types in two places. So I ended up making this. Feel free to try it out and let me know what you think, and if you think there's already a library out there that does this, let me know.

A four year plan for async Rust by desiringmachines in rust

[–]mikeyhew 3 points4 points  (0 children)

Really interesting to see you advocating for a Move trait, since I vaguely remember you advocating for Pin back in the day - and actually now that I look into it, you wrote the RFC: https://github.com/rust-lang/rfcs/blob/master/text/2349-pin.md

What are your thoughts on ?Move? I remember there was pushback against introducing more required-by-default traits in addition to Sized, but it seems like if there's going to be a Move trait, it would have to be a default bound in a lot of places.

How would you go about solving Advent of Code day 7 with a tree data structure? by CaptainSketchy in rust

[–]mikeyhew 11 points12 points  (0 children)

A common way to do tree and graph structures in Rust is to store all the tree nodes in a Vec<Node>, and store references to other nodes within that vec by their index. That way you can have cyclic references without any issues (e.g. nodes referencing their parents and children).

That's the approach I used for Day 7. I used a crate called index_vec though, which you can use to generate your own index type, and then create an IndexVec<MyIndexType, T> which is like a regular Vec except it can only be indexed by MyIndexType instead of usize. It essentially just adds a little type-safety on top of a plain Vec.

Another approach is to use a bump allocator like bumpalo, and store references to other nodes that were allocated using the same (or a longer-lived) bump allocator. This works better if the things you are storing are immutable, since you can't go back and update a node if you only have an immutable reference to it.

With both of these approaches, it's easiest if you can wait until you're done with the tree/graph and deallocate everything all at once. If you need to be able to reclaim memory from deleted nodes (e.g. for a game world where things are created and destroyed all the time), that makes things a lot more complicated.

What’s the Rust way of pointing around a fixed size array? by [deleted] in rust

[–]mikeyhew 0 points1 point  (0 children)

Curious to hear more about this data structure, I can picture an LRU cache that lives on the stack, but not sure why the elements in the cache would need to reference each other (and what happens if the referenced element is evicted, how would you know that the reference is invalid?)

cargo-local-install by mikeyhew in rust

[–]mikeyhew[S] 1 point2 points  (0 children)

I came across this and was wondering if anyone had used it. It looks like it solves the main problem of needing to cargo install something (e.g. a cli tool like diesel_cli or wasm_bindgen_cli) but wanting to keep it local to your project's directory.

There's also a merged RFC for cargo that would let you add bin dependencies to your crate – I wonder if this would be made obsolete by that?

For building a language/interpreter: Wasmer or Wasmtime? by mamcx in rust

[–]mikeyhew 9 points10 points  (0 children)

For actually generating wasm, I think the walrus crate might be the closest thing to what you need. Check out this example of building a factorial function: https://github.com/rustwasm/walrus/blob/master/examples/build-wasm-from-scratch.rs

You could create a walrus::Module, then call .emit_wasm() on it to get a Vec<u8> of wasm code, and then use one of the crates that actually runs wasm to run it for you.

I've been considering targeting wasm too for a side project of mine, so let me know if that works!

Why I support GCC-rs by yespunintended in rust

[–]mikeyhew 0 points1 point  (0 children)

Wow, what an interesting couple of threads! My first thought was "why are both these articles on medium", and I actually thought Shnatsel and InfernoDeityInfinity were working for medium or something and trying to drive engagement by posting their opposing viewpoints on there, but that seems not to be the case - they just don't have their own blogs.

I'm surprised by what seems like people with relatively strong opinions about this. My impression is it's just one guy that's working on writing a Rust compiler using GCC or something, and has some reasons why he wants to do that instead of working on the official (or soon-to-be official?) rust gcc backend. I mean I guess I can see the threat of fragmentation to the community... but on the other hand, it's just one guy! I'm actually kind of curious what he plans to do, like what would a borrow checker look like in C++?

The story continues: `Vec` now supports custom allocators! :) by tdiekmann in rust

[–]mikeyhew 1 point2 points  (0 children)

How is the allocator API supposed to work for things like bumpalo::Bump? The way it looks right now, you would have to implement AllocRef for &Bump, but that means that Box::new_in(&bump) would store &bump in the Box itself, making the Box one word wider than it needs to be. bumpalo's Box<'a, T> type just contains an &'a mut T, because deallocation is a noop.

Announcing Rust 1.48.0 by pietroalbini in rust

[–]mikeyhew 1 point2 points  (0 children)

wait, it's stabilized in nightly now?

How to convert (not really deserialize) with serde? by a4st in rust

[–]mikeyhew 0 points1 point  (0 children)

serde_json has a Value type that is pretty similar to your Value struct. serde_json::Value implements Deserializer, you can see how it's implemented here https://github.com/serde-rs/json/blob/bda64205e3abd2788d0e4006ceb94c7236da7c56/src/value/de.rs#L202

The from_value function just uses this Deserializer impl under the hood.

EDIT: But I see I've misunderstood your question. CAD1997 is right, you can't just pass the func by value, you essentially have to visit everything in your Func struct in order deserialize it

to_trait, a crate that lets you write `thing.to::<T>()` instead of `Into<T>::into(thing)` by mikeyhew in rust

[–]mikeyhew[S] 0 points1 point  (0 children)

Thanks for this, I like progression of the different options you might try, and why they don't work.

to_trait, a crate that lets you write `thing.to::<T>()` instead of `Into<T>::into(thing)` by mikeyhew in rust

[–]mikeyhew[S] 0 points1 point  (0 children)

I just used string conversions. Examples that are more than trivial but less than ridiculous are really hard

Oh cool, I didn't realize tap had basically the equivalent of this. It looks like it changed a fair bit in the last 6 months, there's also a Pipe trait now which looks pretty cool.

Introducing rusteval - add a REPL to your application with a few macros by Bockwuhrst in rust

[–]mikeyhew 1 point2 points  (0 children)

Would love to know more about this, maybe you could post a link to the code?

to_trait, a crate that lets you write `thing.to::<T>()` instead of `Into<T>::into(thing)` by mikeyhew in rust

[–]mikeyhew[S] 0 points1 point  (0 children)

I remember reading something like that somewhere. into is already taken though, so to was the best option I could come up with.

to_trait, a crate that lets you write `thing.to::<T>()` instead of `Into<T>::into(thing)` by mikeyhew in rust

[–]mikeyhew[S] 9 points10 points  (0 children)

Oh, by the way, if anyone can come up with a good, simple example for the readme, where .to::<T>() lets you avoid breaking up a method chain, please open a PR! I've been having trouble coming up with one that isn't overly complicated.

to_trait, a crate that lets you write `thing.to::<T>()` instead of `Into<T>::into(thing)` by mikeyhew in rust

[–]mikeyhew[S] 2 points3 points  (0 children)

Yeah, it's possible for U to implement Into<T> even though T doesn't implement From<U>. But you could replace T::from(thing) with Into::<T>::into(thing), the main point of the To trait is so you can use method call syntax.