Skip to content
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

static mut is ALWAYS a mistake #151

Open
Lucretiel opened this issue May 15, 2024 · 2 comments
Open

static mut is ALWAYS a mistake #151

Lucretiel opened this issue May 15, 2024 · 2 comments
Assignees
Labels
adequate Satisfactory and useful change, issue, discussion enhancement New feature or request rust Backend (Rust) related

Comments

@Lucretiel
Copy link
Contributor

static mut SPOTIFY_CLIENT_ID: String = String::new();
static mut SPOTIFY_CLIENT_SECRET: String = String::new();
static mut AVOID_SPAWN: bool = false;
static mut ACCESS_TOKEN: Option<String> = None;
static mut EXTERNAL_WINDOW: Option<Window> = None;
const CUSTOM_ENGINE: engine::GeneralPurpose =
engine::GeneralPurpose::new(&alphabet::URL_SAFE, general_purpose::NO_PAD);

There are several preferable options here:

  • Use std::sync::OnceLock, a safe way to ensure a global variable is initialized at most once, and then is read-only forever afterwards.
  • use actix_web::web::Data, the standard way to pass data dynamically into a request handler
  • Make your request handler a local lambda, and capture the data you're interested in by reference or by move.
  • In the worst case, use an Arc<Mutex<T>> to pass the data around. This should be your last resort.
@p0ryae p0ryae self-assigned this May 15, 2024
@p0ryae p0ryae added enhancement New feature or request adequate Satisfactory and useful change, issue, discussion rust Backend (Rust) related labels May 15, 2024
@p0ryae
Copy link
Member

p0ryae commented May 17, 2024

Hi @Lucretiel thank you for bringing this up to my attention.

I was curious to know if using a mix of different types of global variables (for example actix_web::web::Data and tauri state) would be a bad practice as well or not. I'm currently in the process of transforming spotify client id and secret into actix_web state data and the other global variables into tauri state data for obvious reasons.

I would appreciate your input on this.

@p0ryae
Copy link
Member

p0ryae commented Jul 23, 2024

Hey @Lucretiel just doing a follow-up :) please read the comment I posted above. I would highly appreciate your input on this matter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adequate Satisfactory and useful change, issue, discussion enhancement New feature or request rust Backend (Rust) related
Projects
None yet
Development

No branches or pull requests

2 participants