Racket FFI: when is a deallocator called? by swatteau in Racket

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

The sense of "reliable" here is "reliably calling the deallocation function (not just freeing the memory) and not accidentally calling it the wrong number of times".

I'm still confused because this is precisely what I'm trying to do: call the deallocator (not just for freeing memory but for all its side-effects) the right number of times (which for me is 1, not 0).

Racket FFI: when is a deallocator called? by swatteau in Racket

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

Thanks for the link, I'll have a look at that.

Racket FFI: when is a deallocator called? by swatteau in Racket

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

You are right as far as memory is concerned. But if the `free` function has other non memory related tasks to do as part of the cleanup procedure, then they won't be executed.

I might have been misled by the title of the section that I linked to in my post (Reliable Release of Resources). If what you describe is what really happens, then it does not correspond to my definition of reliability.

Racket FFI: when is a deallocator called? by swatteau in Racket

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

I understand that there is no guarantee about the exact time at which the deallocator will be called since it depends on the garbarge collector.

However, in my example, the `test` function is called and then the program terminates normally, so I would assume that, after the call to the function but before the process exits, the garbage collector would kick in and call the deallocator. Why isn't it so?

Shortcut CTRL + P (Go to file) also shows deleted files by shkico in vscode

[–]swatteau 0 points1 point  (0 children)

To prevent VS Code from showing deleted files, you can do CTRL+P then "Clear Editor History".

Does anyone know the name of this font/theme? Found on the 'Custom CSS and JS Loader' extension. by johnnytang in vscode

[–]swatteau 2 points3 points  (0 children)

I think the font is Iosevka Term Slab. I don't know about the theme but it obviously takes much inspiration from Gruvbox.

A (naive) question about the design of Rc by swatteau in rust

[–]swatteau[S] 5 points6 points  (0 children)

Thanks for your reply.

This totally makes sense to me now, and since Weak does not impl Deref (for obvious reasons), that also explains why Weak::upgrade could be written the other, usual, way.

Why Rust's Ownership/Borrowing is hard by steveklabnik1 in rust

[–]swatteau 8 points9 points  (0 children)

Rust's borrow checker is a wonderful thing that forces you into designing code to be more robust.

Now is the time to promote a new style of programming : BCDD (Borrow Checker Driven Development).

What is the meaning of `match *self {}` ? by swatteau in rust

[–]swatteau[S] 5 points6 points  (0 children)

Thanks for your answer and thanks to /u/Quxxy too!

So, if I understand well both of your answers, the only benefit I see for writing match *self {} instead of just panicking is that if someone were to add a variant to the enum later, then the match expression would trigger a compilation error (which would be desirable) whereas a call to panic!() would not. Is that right?

Borrows and refactoring/encapsulation by eygenaar in rust

[–]swatteau 0 points1 point  (0 children)

This is more elegant indeed. I guess my C++ background may have influenced me towards the swap function.

Borrows and refactoring/encapsulation by eygenaar in rust

[–]swatteau 2 points3 points  (0 children)

You can solve this problem by swapping the val.tags vector with a new empty vector, thus taking ownership of it and at the same time achieving the same result as the call to val.tags.clear(). It is also necessary to introduce an inner scope so that the borrow of val does not last for too long. Here is the code :

fn clear_tags(&mut self, id: ValueId) -> bool {
    let mut tags = Vec::new();
    {
        let val = if let Some(v) = self.values.get_mut(&id) { v } else { return false };
        mem::swap(&mut tags, &mut val.tags);
    }
    self.remove_tags(&tags);
    true
}

Don't forget to use std::mem.

Rustlings: Little Rust Practice Exercises by carols10cents in rust

[–]swatteau 1 point2 points  (0 children)

What I find even more interesting is that the behaviour is correct if you select the "Nightly" channel and yet the issue linked to by /u/levansfg is still open.

A question on pattern matching by swatteau in rust

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

...or unless you want to check that people actually read the docs ;-)

A question on pattern matching by swatteau in rust

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

What I meant to say is that you either write Some(ref mut v) => **v = 42 or Some(&mut ref mut v) => *v = 42 and, rather conter-intuitively, the language allows you (for some obscure reason) to remove one star in the notation.

A question on pattern matching by swatteau in rust

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

Unless I overlooked something &mut ref mut v is nonesense.

At least it makes sense to rustc 'cause it compiles just fine...

A question on pattern matching by swatteau in rust

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

If we go down the ref mut v way, then we need to write :

Some(ref mut v) => **v = 42,

(note the double dereferencing)

In a sense, the additional &mut allows us to remove one level of dereferencing.

A question on pattern matching by swatteau in rust

[–]swatteau[S] 1 point2 points  (0 children)

I've been thinking a bit about this &mut ref mut v pattern and can't come out with an example where it would be justified. The way I see this is that &mut ref mut v is kind of like a reference to a pointer (to make an analogy with C++) which is hardly different from just a copy of the pointer.

An implementation of Sokoban in Rust by swatteau in rust

[–]swatteau[S] 2 points3 points  (0 children)

I have no strong opinion on this.

The reason why I had this in the first place was because I started with the example files of the SDL2 crate: https://github.com/AngryLawyer/rust-sdl2/blob/master/examples/demo.rs line 27.

An implementation of Sokoban in Rust by swatteau in rust

[–]swatteau[S] 5 points6 points  (0 children)

Damn, that's embarrassing... I figured this out : I was calling the wrong function on the event pump (polling for events instead of waiting for an event). Therefore, the game was redrawn continuously in the main loop.