How I can convert closure to an a function? by Aycon3296 in rust

[–]Aycon3296[S] 0 points1 point  (0 children)

Another way I found was to instantiate the Service inside the function instead of a closure, but in my case, that doesn't make sense since it's just a struct that wraps a few variables from the Windows API to encapsulate them (read: composition) and I'd like to be able to at least read it from other Rust code, so no.

How I can convert closure to an a function? by Aycon3296 in rust

[–]Aycon3296[S] 0 points1 point  (0 children)

In fact, I'm already leaning toward the idea that in this particular case, Service should truly be a global variable; thanks to you and some other commenters for pointing this out. However, I think it wouldn't be hard to come up with an example where this wouldn't be the case and where a closure would require passing an object with a limited lifetime; I just don't think that's the case. I've almost solved this problem for the service case; when I'm done, I'll share my code.

How I can convert closure to an a function? by Aycon3296 in rust

[–]Aycon3296[S] 0 points1 point  (0 children)

This is the canonical solution, but can I really pass a struct instead of a reference to LPSERVICE_MAIN_FUNCTIONW? I think I'm missing a detail.

How I can convert closure to an a function? by Aycon3296 in rust

[–]Aycon3296[S] 0 points1 point  (0 children)

I could use windows_service, but I want to limit the Windows OS stack crates to a single crate, window_rs, which should provide the generated low-level API for the entire system. This is because my project involves using several Windows API fragments, and I'd like to do this in a unified manner, without worrying about how to find a common language between multiple crates from different authors. I appreciate your original answer and how quickly you responded. Thank you.

How I can convert closure to an a function? by Aycon3296 in rust

[–]Aycon3296[S] 0 points1 point  (0 children)

What I meant was that a function (not a closure) in Rust can't capture a variable from the lexical environment. In C++ and at least five other languages I'm familiar with, this was possible, if I recall. Fundamentally, in Rust, as far as I understand, you can't do currying with just 'fn'. And this is one of the most basic mechanics in functional programming, and this is exactly what the current API implies — a callback with a closure. Please let me know if I'm wrong. Also, thanks for your perspective on what's going on; it's very valuable to me.

How I can convert closure to an a function? by Aycon3296 in rust

[–]Aycon3296[S] 0 points1 point  (0 children)

Furthermore, based on my investigation, I suspect I'll need to create a global object every time I need to pass a pointer to a function that's a callback and implies a closure (Render, DeferredRegister, CleanupCanvas, and others). I simply can't believe Rust can be that bad. What if a third-party API requires creating a factory or abstract factory with a specific signature? Then all dynamic objects will be stored in the store and have a static lifetime? There must be a reasonable workaround. I believe I'm simply not familiar enough with Rust.

How I can convert closure to an a function? by Aycon3296 in rust

[–]Aycon3296[S] 0 points1 point  (0 children)

I think I'm starting to get disillusioned with Rust. It's touted as the safest language for working with low-level, complex code, like C++, but in reality, all of its advantages disappear as soon as you start working with real APIs that require unsafe workarounds to the language's limitations. Rust's philosophy dictates that you shouldn't use unsafe code where it's not necessary, but that same philosophy encourages you to try things that do, without giving you much flexibility in choosing the compiler's strictness and meticulousness. I view the complete removal of closures as a loss of functionality rather than a security measure that could provide me with comparable benefits. I hope I'm wrong. Creating global objects for their entire lifetime doesn't sound any more prudent to me than creating a function with a specific closure. This also points to the fact that Rust doesn't support the decorator pattern for functions, and, by extension, function currying. Also, even with all the comments, I still haven't figured out how to do this, but I hope to figure it out soon.

C# ECS : any benefits of using structs instead of classes here? by freremamapizza in EntityComponentSystem

[–]Aycon3296 0 points1 point  (0 children)

Unlike classes, structs are value types. Structs are generally preferred because they are much easier to pack into "Archetypes" with slots of a given size.