Skip Navigation

InitialsDiceBearhttps://github.com/dicebear/dicebearhttps://creativecommons.org/publicdomain/zero/1.0/„Initials” (https://github.com/dicebear/dicebear) by „DiceBear”, licensed under „CC0 1.0” (https://creativecommons.org/publicdomain/zero/1.0/)C
Posts
0
Comments
522
Joined
3 yr. ago

  • RefCell is neither mutable nor immutable. It's a type like any other. What is special about RefCell is that it has a method like:

    fn borrow_mut(&self) -> &mut T

    Which means you can get a mutable reference to its contents by only having a normal reference to the RefCell.

    By pointers I mean raw pointers. The pointers themselves are not unsafe. They are just normal pointers like you would have in C.

    Rc can be used to generate weak refs. Which is what you want for your tree.

    I don't know about servo. So I can't tell you much about it.

    Don't hope too much about the temporary unsafe thing. It's not practical (maybe impossible) to make a safety checker that checks the entire program. The practical way is to make safe abstractions using unsafe code. For example rust itself is built upon plenty of unsafe code, however, it provides to you the appropriate abstractions so what you do is safe.

    In this case. You can have a bit of unsafe code on your tree, so that the users of that tree have a safe API to do something that you needed unsafe code for.

    For example one of the cases where you cannot automatically check the safety is in dereferencing a raw pointer returned by a FFI function call to a dynamic library. Your automatic safety checker would need to be able to read the library's documentation. And at that point it's not a real safety checker because the documentation may lie or have bugs.

  • The safe, fast and easy way to do trees is by using Rc<RefCell

    <T>

    >. Rc/Arc allows data to be owned multiple times. You want this because this way a node can be referenced by its parent and its child at the same time. However, Rc makes the inner type inmutable. And you probably will want to mutate it in a tree, that's what RefCell is for. With RefCell you do the borrow checking at run-time instead of at compile-time. This allows you to mutate T even though Rc only gives you an inmutable reference. This is called interior mutability.

    RefCell doesn't eliminate the borrow checker though, you must still follow its rules. If you try to get 2 mutable references to the inner type of RefCell, it will panic.

    I know you don't want to read unsafe, but you gotta hear about the alternative. Just use pointers. Pointers don't have the borrow checker to restrict them. And self-referencing structures with interior mutability are not easy to borrow-check automatically. You can have the raw pointers as private fields of the struct so the code that is actually unsafe will be a few very small functions.

    Here's why the other options worse than pointers:

    Rc<RefCell

    <T>

    > will clutter your code with boilerplate and it's a pain to deal with. Pointers are not too ergonomic in rust (mainly because there is no -> operator), but they need way less boilerplate. Also, you already need to manually check the mutability rules, why not all the rules.

    Another option that I've seen is "have a hashmap with all the nodes, and just store the id of the node instead of a reference". This is the same as "have all the nodes on a Vector and store the index". Think about this for a second. You have a pool of memory and a number that identifies what part of that pool is the memory you want. Seen it yet? That is exactly what a pointer is! If you do that, you're just disabling the borrow-checker anyway. You just created your own memory allocator and will have to manage your memory manually, at that point just use pointers, it will be the same except with fewer boilerplate and indirection.

  •  rust
        
    unsafe fn<'a, T>(p: &'a T) -> &'static mut T {
        p as *cons T as *mut T as &'static mut T
    }
    
    
      

    It is a bit more than just dereferencing raw pointers.

  • I don't think it's true that it works best by doing bottom-up. I develop top-down all the time.

    Whenever you need a new function/method/struct field/enum variant, just write its name where you are going to use it. Then intellisense will complain that it doesn't exist. Press Ctrl+. (Or whatever you have keybinded to "apply suggestion") And now it suddenly exists and intelligence works perfectly fine. It will just place a todo!() or raise UnimplementedException or whatever so you don't forget to implement it later.

  • We have to experiment with mice before achieving the ultimate goal of laser shooting sharks.

  • Vs codium is a FOSS vs-code

  • This point is always stated about XML as if it were the most important part of choosing XML.

    But jsonschema exists. It has the same capability.

  • The const hash map is huge. It was always a pain the have const/static hash maps. Specially since most use cases don't need a DoS-resistant hash map. Will be migrating to 2024 as soon as possible.

  • I didn't say that it's not used. I say that you shouldn't if you have the option.

    If the entire X world uses Y technology. You have no choice other than using Y technology.

  • I don't think anyone actually chooses XML. There's no reason to use it over JSON unless you need to.

  • Considering that using a keyword to name anything results in compiler (or worse! Interpreter) errors, and that libraries are a thing. And also that copy-pasting code from the internet is a thing. I don't think it would be a good idea to localize programming languages.

  • I don't understand the complaint of "subpar IDE support".

    It has the best IDE support of any language I've tried. It's IDE support is also just a language server, which you can just download in your editor of choice and it just works. Are the people that complain about it not using rust-analyzer?

  • Your fellow man is probably a nazi if he bought a cyber truck.

  • Until I read this comment I was 100% certain the post was about short Germans somehow preferring having their balconies occluded by taller-than-them solar panels.

  • Just ask your ISP for more upload speed (and pay for it). It's a thing you can do.

  • Also, most importantly. Assembly.

  • JavaScript

  • It used to be a serious offense to stage a coup.

  • If you need to use bash a lot just to learn 2 "keywords", then it's not a good language.

    I have looked at bash scripts in the past, and even written some (small amount). I had to look up -z and -n every time. I've written a lot more python than bash, that's for sure. But even if I don't write python for a year, when needed I can just write an entire python script without minimal doc lookups. I just need to search if the function I want is part of syd, os or path.

    The first time I want to do an else if my IDE will mark it red and I'll write eliffrom then on, same thing if I try to use { }.

    If a bash script requires at least one array and one if statement, I can write the entire thing in python faster than I can search how to do those 2 things in bash.