Hi, I found some problems with your implementation of the Singleton pattern, so I made changes to make it similar to how the lazy_static! macro works and howstd::io initializes Stdin/Stdout/Stderr.
Use MaybeUninit instead of Option. Tells the compiler to reserve enough uninitialized memory for a Mutex without having to deal with Option. This is safe as long as we can guarantee that we initialize the Mutex before returning it.
Use Once to prevent multiple threads leaking memory by initializing CONF at the same time.
Return a 'static reference instead of using an Arc. A static variable is valid for the lifetime of the program, so the reference count would never drop below 1. We can just dereference the pointer (&*) to make as many references as we want.
Hi, I found some problems with your implementation of the Singleton pattern, so I made changes to make it similar to how the lazy_static! macro works and how
std::io
initializesStdin
/Stdout
/Stderr
.MaybeUninit
instead ofOption
. Tells the compiler to reserve enough uninitialized memory for aMutex
without having to deal withOption
. This is safe as long as we can guarantee that we initialize theMutex
before returning it.Once
to prevent multiple threads leaking memory by initializingCONF
at the same time.'static
reference instead of using anArc
. A static variable is valid for the lifetime of the program, so the reference count would never drop below 1. We can just dereference the pointer (&*
) to make as many references as we want.