AviadCohen24 / RustDirectorySniffer

High-performance Rust DLL for efficient folder hierarchy scanning and manipulation, with seamless C/C++ integration.
2 stars 0 forks source link

Bad API relies on global state #1

Closed BigBIueWhale closed 10 months ago

BigBIueWhale commented 10 months ago

lazy_static! considered harmful. Better off to use a "openRustDirectorySniffer" "closeRustDirectorySniffer" design pattern, that returned a handle to the state, which is then managed by this library.

AviadCohen24 commented 10 months ago

Thank you for your insightful suggestion regarding the use of lazy_static! in Rust. I've taken your advice to heart and restructured the code to implement the "openRustDirectorySniffer" and "closeRustDirectorySniffer" design pattern. This approach has indeed provided a more elegant and manageable way to handle state within the library. Additionally, inspired by your input, I've added a 'stop scanning' functionality, enhancing the control and efficiency of directory scanning processes. Your guidance was invaluable, and the code is now more robust and maintainable thanks to the changes made.

BigBIueWhale commented 10 months ago

That is exactly what I meant. This is a common design pattern in C programming when trying to simulate OOP: You return a pointer to an opaque struct (essentially a void*), and the library is also responsible for freeing that opaque struct. This is good for compatibility with C APIs and it's also good practice that if a dynamic library allocates memory, it should be deallocated by the library itself, to avoid issues with custom allocators or different C runtimes.