-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tracking Issue: What do you use static mut
for?
#1
Comments
Coming here from the Matrix chat. For me there are three main uses of
Hope it helps |
I've use them once in a while if I want to prove something to me. For example, I'm inspecting some generated assembly and I need a write to not be optimized away. A static mut is a quick way to get that in place of a let mut. Another thing I can imagine, but haven't done myself so far, is having to have a global variable for compat with C. Say a C lib has a global extern variable, how would you create that un Rust without static mut? |
@spcan that definitely helps! Grounded already has some tooling for getting a pointer to a static uninit item or array of items, that I believe to be more sound of an abstraction to build on top of (check out I definitely want to add items (or examples) that show how to use it with specifically defined linker sections, in case it needs to be mapped at a specific location. Additionally:
Just nitpicking, the compiler is allowed to assume that undefined behavior never exists, and this can cause it to optimize your program in unexpected ways, including removing whole chunks of it, even if you use Similarly to @diondokter, in theory a static mut should be no more resistant than any other mut variable to being optimized out. Though if it's a single threaded example and you are very careful to not create aliasing references, it shouldn't be immediately UB (just very eas to accidentally cross the line).
From my answer in chat:
|
Also, thank you both for sharing! I do want to share why I want to provide other options, and both of your examples help a ton on what kinds of structures, docs, and examples I should provide! |
I'd love to get rid of this one: https://github.com/ferrous-systems/rust-exercises/blob/7e6d9e6029ad9a31b0c75d233dda6921cd8421f0/nrf52-code/boards/dongle/src/lib.rs#L294 It's a |
@thejpster your example is nearly exactly the use case of I've avoided duplicating it because static cell is already well implemented and widely used. |
Neat, I'll just use that then. |
I use Looks something like this: static const struct app_callbacks user_app_callbacks = {
.app_on_connection = user_app_connection,
.app_on_disconnect = user_app_disconnect,
.app_on_update_params_rejected = NULL,
.app_on_update_params_complete = NULL,
.app_on_set_dev_config_complete = default_app_on_set_dev_config_complete,
.app_on_adv_nonconn_complete = NULL,
.app_on_adv_undirect_complete = user_app_adv_undirect_complete,
.app_on_adv_direct_complete = NULL,
.app_on_db_init_complete = default_app_on_db_init_complete,
.app_on_scanning_completed = NULL,
.app_on_adv_report_ind = NULL,
.app_on_get_dev_name = default_app_on_get_dev_name,
.app_on_get_dev_appearance = default_app_on_get_dev_appearance,
.app_on_get_dev_slv_pref_params = default_app_on_get_dev_slv_pref_params,
.app_on_set_dev_info = default_app_on_set_dev_info,
.app_on_data_length_change = NULL,
.app_on_update_params_request = default_app_update_params_request,
.app_on_generate_static_random_addr = default_app_generate_static_random_addr,
.app_on_svc_changed_cfg_ind = NULL,
.app_on_get_peer_features = NULL,
#if (BLE_APP_SEC)
.app_on_pairing_request = NULL,
.app_on_tk_exch = NULL,
.app_on_irk_exch = NULL,
.app_on_csrk_exch = NULL,
.app_on_ltk_exch = NULL,
.app_on_pairing_succeeded = NULL,
.app_on_encrypt_ind = NULL,
.app_on_encrypt_req_ind = NULL,
.app_on_security_req_ind = NULL,
.app_on_addr_solved_ind = NULL,
.app_on_addr_resolve_failed = NULL,
#if !defined (__DA14531_01__) && !defined (__DA14535__)
.app_on_ral_cmp_evt = NULL,
.app_on_ral_size_ind = NULL,
.app_on_ral_addr_ind = NULL,
#endif // not for DA14531-01, DA14535
#endif // (BLE_APP_SEC)
}; ps. Perhaps there is a better way (with less statics) to integrate with the SDK I'm using. |
If you have an example of why you use/need
static mut
, particularly in an embedded context, I'm interested in hearing why! This will help me provide safer alternatives ingrounded
in the future.If you can share a link to code, or a snippet inline or on the Rust Playground, that would help out a ton.
The text was updated successfully, but these errors were encountered: