32 questions
1
vote
0
answers
67
views
Print lazy_static! variables in GDB?
I am writing a program in Rust and when debugging in GDB, I noticed that the variables defined within the lazy_static! macro were not displaying as expected.
#[derive(Clone, Copy)]
struct Addr(usize);
...
0
votes
1
answer
69
views
'cannot borrow `tree` as mutable more than once at a time' while using lazy_static
#[macro_use]
extern crate lazy_static;
extern crate alloc;
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
fn get_vec<'a>(tree: &'a mut BTreeMap<&'a str, Vec<&'a u8>...
1
vote
1
answer
133
views
Why is the size of a lazy_static! variable in the symbol table zero?
I have the following Rust code targetting x86_64:
#[macro_use]
extern crate lazy_static;
use std::collections::HashMap;
lazy_static! {
static ref HASHMAP: HashMap<u32, &'static str> = {...
0
votes
1
answer
189
views
Store an object in a static Box and then retrieve as mutable?
This is a PyO3 situation. So the Python calls a long-running task. Using ZeroMQ messaging (inter-process messaging*) it is possible for the user to order the task to be suspended. At that point I want ...
0
votes
1
answer
1k
views
Lazy initialisation of lazy_static?
I've already had some success with lazy_static:
static ref WORD_COUNT_REPORTING_STEP_MUTEX: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
static ref INDEX_NAME: RwLock<String> = ...
0
votes
1
answer
191
views
Why does this return statement cause an error but the return expression doesn't in Rust?
I'm confused why this code works below:
lazy_static! {
static ref TSS: TaskStateSegment = {
let mut tss = TaskStateSegment::new();
tss.interrupt_stack_table[...
0
votes
0
answers
140
views
Stateful embedded library in Rust
I want to develop a library in Rust for microcontrollers that holds some state information.
This state data cannot be transferred to the caller of the library. I am using #![no_std]. The library ...
0
votes
2
answers
205
views
no rules expected this token in macro call during initialization of static variable using lazy_static crate
I am trying to initialize a static variable at runtime using lazy_static crate. But I'm getting no rules expected the token E1 error while compiling. This is the link lazy_static I followed
use ...
-1
votes
1
answer
365
views
How to preload lazy_static variables in Rust?
I use Rust's lazy_static crate to assign a frequently used database object to a global variable but I do not want lazy loading. Is there a way to trigger lazy_static to preload the variable or is ...
2
votes
1
answer
2k
views
SQLx MySQL connection with lazy_static cannot be sent between threads safely
I use sqlx for initializing mysql connection (asynchronous) using lazy_static but weird errors occurred.
This is the code that I wrote:
use actix_web::middleware::Logger;
use actix_web::web::{Data, ...
1
vote
1
answer
4k
views
Rust: initialize a static variable/reference in a lib?
I'm new to Rust. I'm trying to create a static variable DATA of Vec<u8> in a library so that it is initialized after the compilation of the lib. I then include the lib in the main code hoping to ...
23
votes
2
answers
19k
views
Are there still reasons to use lazy_static? [closed]
lazy_static is a very popular crate. Years ago, it had no better alternatives for certain tasks. But today, are there still any reasons to choose lazy_static over OnceCell (from the standard library, ...
0
votes
1
answer
605
views
The item's lifetime of global HashMap by lazy_static in Rust
I am new to Rust.
I define a global HashMap of User by lazy_static.
There is a lifetime in User, so I have to set a lifetime in lazy_static. It seems that only 'static can be used in lazy_static.
Here ...
3
votes
2
answers
2k
views
How do I free memory in a lazy_static?
The documentation states that if the type has a destructor, it won't be called: https://docs.rs/lazy_static/1.4.0/lazy_static/#semantics
So how am I supposed to free the memory?
1
vote
1
answer
485
views
How to mutate static ref usize?
lazy_static! {
static ref MY_GLOBAL: Mutex<usize> = Mutex::new(100);
}
MY_GLOBAL.lock().unwrap() += 1;
This code gives me these errors:
cannot use `+=` on type `MutexGuard<'_, usize>`
...
2
votes
0
answers
896
views
Deferred initialization and mutable static borrowing [Rust]
My question is the following:
Would it be possible to make a deferred initialization of an object and then borrow it as mutable for a 'static lifetime?
Some context
First of all, I am going to show ...
1
vote
1
answer
1k
views
Filling a static array from different modules in Rust?
In my Rust project, I need a globally hold, static array or vec that is initialized once where modules can register values or functions on. I thought, this would be possible using the lazy_static!-...
0
votes
1
answer
780
views
Get item's reference from global HashMap in Rust
I'm trying to use a static HashMap<String, Object> to store some data that I want to use and modify globally in the future. I found out that some way of declaring such a global map is by using ...
0
votes
0
answers
44
views
Rust: Why is lazy_static not working with some types [duplicate]
I need to create some global mutable variables, using lazy_static! with some types works, but with some other types it doesn't. The mutex is to change value.
This works:
lazy_static! {
static ref ...
0
votes
1
answer
1k
views
What type should I use to return a lazy_static value?
I plan to have a struct which provides the JSON schema via a trait method.
The schema is stored compiled in a lazy_static variable, but which type does my schema() function have to return?
lazy_static:...
1
vote
0
answers
421
views
Accessing disjoint entries in global HashMap for lifetime of thread in Rust
my current project requires recording some information for various events that happen during the execution of a thread. These events are saved in a global struct index by the thread id:
RECORDER1: ...
3
votes
1
answer
952
views
Define a static boolean in Rust from std::env::consts::OS=="windows"
I'd like to create a global static boolean value called IS_WINDOWS in a Rust file:
lazy_static! {
pub static ref IS_WINDOWS: bool = std::env::consts::OS=="windows";
}
However, when I do ...
3
votes
1
answer
2k
views
How to insert a String variable to a global mutable HashMap(using lazy_static and Mutex) without causing "does not live long enough" problem?
I am using Rust and I want to use a global mutable HashMap for convenience. However, while it is possible to define a global, mutable HashMap using lazy_static and Mutex, it is hard for my String ...
2
votes
1
answer
3k
views
Rust lazy_static and tokio::sync::mpsc::channel in tokio::select
I started coding with rust lately and I'm loving it. I'm coding on a project where I want to "wrap" a C-API. In one case I have to define callbacks in Rust, which C can call. I let bindgen ...
3
votes
1
answer
647
views
The initialization function is called twice in a `lazy_static` block
I have a big project Where I use lazy_static to create a singleton. I think there is a bug in lazy_static crate (which appears in big projects only) or I am doing something wrong because the ...
1
vote
1
answer
2k
views
What is better in Rust: defining a static variable to access it globally or passing a non-static variable through function arguments? [closed]
If a variable is used in multiple functions in a file, is it better to define it as static rather than passing a non-static variable through function arguments? Does this have a performance impact?
An ...
0
votes
1
answer
2k
views
Rust lazy_static variable RwLock access
I am trying to declare and read/write an instance of a custom struct, using lazy_static as I had to use non-const function at its initialization (string).
As I saw here in an other Stackoverflow post, ...
1
vote
2
answers
2k
views
Rust lazy static custom struct instance
In Rust, I am trying to declare a static instance of a custom struct.
Because by default I am not able to assign other values than const ones, I am trying to use lazy_static.
Here is my custom struct:
...
2
votes
2
answers
639
views
Translating Cpp to Rust, handling a global static object
I'm a beginner translating a familiar Cpp project to Rust. The project contains a class called Globals which stores global config parameters. Here is an extract from its cpp file:
static Globals &...
4
votes
2
answers
2k
views
Return a reference to a T inside a lazy static RwLock<Option<T>>?
I have a lazy static struct that I want to be able to set to some random value in the beginning of the execution of the program, and then get later. This little silly snippet can be used as an example:...
6
votes
4
answers
4k
views
alternative to using 'await' with lazy_static! macro in rust?
I want to use Async MongoDB in a project.
I don't want to pass around the client because it would need to go around multiple tasks and threads. So I kept a static client using lazy_static. However, I ...
3
votes
2
answers
1k
views
How can return a reference to an item in a static hashmap inside a mutex?
I am trying to access a static hashmap for reading and writing but I am always getting error:
use std::collections::HashMap;
use std::sync::Mutex;
pub struct ModuleItem {
pub absolute_path: ...