Good vs. Evil

Bigdbigd03

New Member
Jul 29, 2019
159
30
0
49 come to the dark side. Become my apprentice. I will help you expel your anger, let out your frustration, and become a better person.
 

GamerwithnoGame

Over-Achiever
Jan 29, 2015
2,808
1,507
224
50. I serve something different, something unknowable to the rage-ridden disciples of Dark and the peace-preaching lovers of Light.
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,799
248
49 I implemented the change that I wanted. So, now layers are automatically removed when you loose access to them. Which was actually rather easy and is a nice showcase of why rust is awesome. That is because a lot of languages can't do this automatically, like javascript and python. Some languages, like C# make you think you can implement that, but when you look into it you learn that it will not behave as you want and from the few where you can actually implement this you need to manage the memory yourself, meaning that you can still mess it up.

While in rust all I had to do was to switch to a custom type that had an implementation for Drop, send it a message to the context and handle this message. To implement Drop is just 5 lines of code and to handle the message is a simple match statement

Code:
impl Drop for LayerId {
    fn drop(&mut self) {
        let _ =self.channel.send((self.id, LayerInstructions::Drop));
    }
}

Reason why languages like C# make you think you can do this: C# also allows you to run code when a value gets removed out of memory, however when exactly this happens is dependent on when the garbage collector removes it, not when all references to a value are gone. This means that it can run anywhere between immediately after you loose access to it until your program is closed. Rust meanwhile, is aware of when things aren't needed any more at compile time and is thus able to run the drop method as soon as it isn't needed any more.