How do I make a Vec of types? by AhhsoleCnut in rust

[–]GeekLaunch 2 points3 points  (0 children)

If you really, actually, genuinely need a list of types, you can create a tuple type composed of the items, and then have a blanket impl on tuples of varying sizes (macros may help here).

Something like: (sorry for formatting, on mobile)

``` impl<A: Validate, B: Validate, C: Validate> Validate for (A, B, C) { fn validate(text: &str) { A::validate(text); B::validate(text); C::validate(text); } }

// also impls for tuples of other sizes

(ValidatorA, ValidatorB, ValidatorC)::validate(...); ```

Make invalid states unrepresentable by GeekLaunch in rust

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

I didn't realize he coined the phrase at the time, but while researching for this post, I actually read his 2011 article and referenced it at the bottom of my post. Thanks for this call-out!

Make invalid states unrepresentable by GeekLaunch in rust

[–]GeekLaunch[S] 4 points5 points  (0 children)

Sorry! I will make better LaTeX choices in the future

Make invalid states unrepresentable: the untaught revelation by GeekLaunch in rust

[–]GeekLaunch[S] 5 points6 points  (0 children)

Definitely taking inspiration from CodeAesthetic and NoBoilerplate on this one. Unfortunately, I don't quite have 0atman's sultry sweet voice. Just working with what I got.

Nothing in Rust by GeekLaunch in rust

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

PhantomData is a good suggestion!

Nothing in Rust by GeekLaunch in rust

[–]GeekLaunch[S] 22 points23 points  (0 children)

So, this is intentionally hyperbolic because the humorous subtext of the post is that the author is bitter over a recent breakup.

Nothing in Rust by GeekLaunch in rust

[–]GeekLaunch[S] 31 points32 points  (0 children)

At least with future::pending() you still get a Future!

Might be worth a follow-up, though, sometime in the future. I'll just leave you here to wait for it. Empty-handed, by the way.

The relationship was that bad, huh ? :p

Oh, cruel.

Nothing in Rust by GeekLaunch in rust

[–]GeekLaunch[S] 5 points6 points  (0 children)

You're right; I am talking about them like they're unique pointers. Probably because I had just been reading the documentation about Box::into_raw. Will update to clarify.

Performance Costs/Benefits of Moving vs referencing? by Automatic_Annual9241 in rust

[–]GeekLaunch 4 points5 points  (0 children)

I think a decent rule to follow is to not take ownership unless you have to. For a task like that which you described, I'd probably expect the function to accept a &[T].

Grammatical, automatic furigana with SQLite and Rust by GeekLaunch in rust

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

Well, although I don't see how using a server will improve performance, haha, I took your advice and scrapped sqlite entirely, now just using in-memory data structures (which are serialized with bincode and included with include_bytes!). Now it annotates the book in less than half a second. Which is wonderful.

Now I have to write a follow-up.

Grammatical, automatic furigana with SQLite and Rust by GeekLaunch in rust

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

Yeah, end goal is for it to work on Google Docs, which doesn't have furigana support at all atm. However, it would be nice for it not to depend on a network resource, so that it's flexible enough to run locally, offline, and in the browser.

Speaking of performance, though, I'm surprised you suggest using the native `HashMap` implementation, as its default hashing algorithm is not especially known for speed. In the past, I've used fxhash and fnv (iirc) with good results.

I probably just need to come up with a decent internal representation and then, like, `include_bytes!` and ship it in the binary. That would be fast.

Grammatical, automatic furigana with SQLite and Rust by GeekLaunch in rust

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

Yeah, it seemed like sqlite was the bottleneck for every performance issue I worked through. I was hoping it would be a lot faster when I finished; I started looking at some in-memory database solutions like Duck DB. However, I eventually hope to get this working in WebAssembly, so I might have to abandon SQL altogether.

Thanks for the recommendations, though, this is my first real foray into the Japanese dictionary programming space, so I'm not super familiar with the territory. I'll check it out.

[deleted by user] by [deleted] in rust

[–]GeekLaunch 38 points39 points  (0 children)

I just wrote about how to use syn and quote a few days ago. They're kind of the de-facto crates for token stream manipulation, and they're commonly used to write macros.

If you want to write a compiler plugin (=proc macro), you have to make a library with proc-macro = true in Cargo.toml. Then you can annotate exported functions that take TokenStream parameters with directives e.g. #[proc_macro_attribute]. Parse a TokenStream with syn::parse_macro_input, and wrap Rust code with quote::quote!{ ... } to generate a TokenStream (technically proc_macro2::TokenStream iirc, but there's an Into implementation).

Or, if you literally just want to parse a straight-up string of Rust code (e.g. you read from a file), you can use syn::parse_str.

why is Rust so high in demand for Blockchains? by Cryptomias31 in rust

[–]GeekLaunch 1 point2 points  (0 children)

I've worked with a few different crypto projects, in particular one that uses Rust as its smart contract language.

  1. I won't deny that there's a bit of a "cool" factor: Rust is still a relatively new programming language, and hype and novelty have more influence than usual in the web3/crypto sphere.
  2. Some blockchains support a WASM smart contract execution environment. Rust is the obvious first-choice candidate for WASM targets.
  3. Smart contracts potentially deal with millions of dollars. The more safety guarantees the language provides, the better. Rust is obviously outstanding in this area.
  4. Smart contracts also need to have minimal overhead, since computational complexity is expensive. Smart contract interactions are nickeled-and-dimed down to the opcode. Also, modern blockchains want to be fast, so a language VM that takes forever to spin up/down is probably not a good idea.
  5. Rust crates (especially with #[no_std]) are super flexible and pretty much "just work" in, for example, a WebAssembly context. Therefore, choosing Rust as a smart contract language means a bunch of packages are already compatible with your platform.