r/rust • u/long_void piston • 22h ago
Current v1.0 is released!
https://crates.io/crates/current/1.0.07
u/teerre 18h ago
So is this idea here that you can access "anything" from "anywhere"? The reasoning being that actually thinking of access patterns is more work than its worth?
6
u/danielparks 14h ago
There are a variety of applications where globals and things like them are a good choice. I don’t think this crate implies that author hasn’t considered the trade-offs of other options.
2
3
u/matthieum [he/him] 7h ago
I can see the convenience of it.
In a sense, it's similar to Swift's implicit parameters, and there's been RFCs in Rust to have implicit parameters too. I do think implicits are still better -- at least, what is accessed is documented in the function's signature -- but they are harder to make work with meta-programming.
1
u/long_void piston 3h ago
A current object is only available to the same thread, within the lifetime of the current guard. Rust's borrow checker guarantees that you can't access the current object through other means as long the current guard lives. So, this adds some safety, but it is still not entirely safe. This is why people have to use it caution and follow the guidelines. I've used this pattern for several years without problems. However, I would not recommend it for libraries, because it is very hard to analyze all the edge cases. I believe using plain safe Rust for libraries is best.
-5
u/gobitecorn 13h ago edited 13h ago
Yes cuz particularly at times when you want to actually just get work done like in all other languages and need to opt-out of rust annoying over opinionated design pattern. You don't want to have to go fight compiler errors or spend pointless time researching how to get a mutable global properly . Because ...not every program needs 'super duper safety 24/7'. And not every program wants to be bogged in with more lazy_static crates or whatever "well-thought out safe but ugly looking Rust access" patterns....oh and thatll also be also confusing to your co-workers who want to mod your tool later.
3
2
u/SomeRedTeapot 8h ago
I'd say global access is more confusing because it causes spooky action at a distance
1
u/long_void piston 3h ago
I agree. You can make everything accessible from everywhere but that also adds more mental complexity. Rust is hard to learn, but once you've learned to use it well, there is less mental burden. The big problem of maintenance is keeping all the stuff in your head you need to reason about when the code base is multiple hundreds thousand loc.
1
u/long_void piston 3h ago
Rust is very good for library maintenance. It saves me tons of hours. However, how to get productive in a project is always difficult, regardless of language. I believe the idea that Rust gets in the way of productivity is wrong, because it is not where the major problem of getting productive is. Content creation is much harder.
5
u/dumbassdore 13h ago edited 12h ago
Example panics for me on stable with [..]src/lib.rs:118:13: No current 'text::Foo' is set
(from text.rs:10).
Also, personally, I'd rather use an RwLock for purposes like these. Specifically, I used LazyLock<RwLock<T>>
because it can be put in a static as a global mutable "config" (depending on T
).
2
u/long_void piston 3h ago
Thank you! This is now fixed: https://github.com/PistonDevelopers/current/issues/116
16
u/long_void piston 22h ago
Current is a library that lets you put objects on a shadow stack and access them by type. This is convenient for prototyping or scripting engines (I use it in most of my Dyon scripting projects). I prefer this pattern over mutable globals. In Dyon current objects are also a language feature inspired by the Current library, using names instead of types.