• úvod
  • témata
  • události
  • tržiště
  • diskuze
  • nástěnka
  • přihlásit
    registrace
    ztracené heslo?
    SPIRALIRust - Programovací jazyk
    SPIKE411
    SPIKE411 --- ---
    Avoiding allocations in Rust to shrink Wasm modules
    https://nickb.dev/blog/avoiding-allocations-in-rust-to-shrink-wasm-modules/
    SPIKE411
    SPIKE411 --- ---
    Rust programming language outlines plan for updates to style guide | ZDNET
    https://www.zdnet.com/article/rust-programming-language-outlines-plan-for-updates-to-style-guide/
    SPIKE411
    SPIKE411 --- ---
    Rust programming language: Driving innovation in unexpected places | ZDNET
    https://www.zdnet.com/article/programming-languages-why-these-developers-like-rust-in-their-cars/
    SPIKE411
    SPIKE411 --- ---
    Hot Reloading Rust — for Fun and Faster Feedback Cycles | Robert Krahn
    https://robert.kra.hn/posts/hot-reloading-rust/
    SPIKE411
    SPIKE411 --- ---
    PrettySize 0.3 release and a weakness in rust’s type system
    https://neosmart.net/blog/2022/prettysize-0-3-release-and-a-weakness-in-rusts-type-system/
    SPIKE411
    SPIKE411 --- ---
    GitHub brings supply chain security features to the Rust community | The GitHub Blog
    https://github.blog/2022-06-06-github-brings-supply-chain-security-features-to-the-rust-community/
    SPIKE411
    SPIKE411 --- ---
    SPIKE411
    SPIKE411 --- ---
    GitHub - quambene/rust-concurrency: Rust Concurrency Cheat Sheet
    https://github.com/quambene/rust-concurrency
    SPIKE411
    SPIKE411 --- ---
    SPIKE411
    SPIKE411 --- ---
    Rust's Unsafe Pointer Types Need An Overhaul - Faultlore
    https://gankra.github.io/blah/fix-rust-pointers/
    SPIKE411
    SPIKE411 --- ---
    Writing Rust the Elixir way - 18 months later | Lunatic
    https://lunatic.solutions/blog/writing-rust-the-elixir-way-1.5-years-later/
    BONEFLUTE
    BONEFLUTE --- ---
    Ahoj. Rád bych do svého projektu zakomponoval scriptovací jazyk na řešení handlerů a rutin. V repozitářích je několik zajímavých projektů. Konkrétně mě zaujali: https://crates.io/crates/gluon a https://crates.io/crates/dyon
    Máte s tím nějaké praktické zkušenosti? Nebo i s jinými?
    BONEFLUTE
    BONEFLUTE --- ---
    SPIRALI: Moc děkuji za vysvětlení.
    SPIRALI
    SPIRALI --- ---
    BONEFLUTE: Cilem hry je ziskat:
    (1) Uvoleneni zdroju kdyz uz je nikdo nevyuziva
    (2) Zabranit tomu aby nekdo ziskal zaroven read and write referenci nebo dve write.

    Rc resi (1) tim ze ma counter ktery pocita kolik je uzivatelu te reference, kdyz spadne na 0 tak uvolnuje. "get_mut" na Rc pak ve specialnim pripade kdy counter ma hodnotu prave 1 umoznuje vratit &mut referenci, protoze tim ze vime ze jsme jedini kdo ma tuto referenci, tak je to bezpecne i vzhledem k (2). Pokud uz ale Root ma alespon dva Itemy tak Rc counter bude alespon na 2 a tedy get_mut selze (i kdyz nikdo zrovna nema read/write pristup). Proto je tam nutne dat RefCell, ktery slouzi k reseni (2) a zavede dalsi counter, ktery pocita rozpujcovane reference.

    Zkracene: Rc pocita kolik existuje mist na kterych o nas vedi, RefCell pocita kolik mist cte, pripadne jestli do nas nekdo zrovna nezapisuje.
    Oboji idealne hlida prekladac v dobe prekladu, ale nekdy to nejde vyjadrit a je treba tyto kontrolu shodit pomoci Rc/RefCellu do runtimu.
    BONEFLUTE
    BONEFLUTE --- ---
    SPIRALI: Ou! Paráda. A já se s tím tady mořím.
    Díky moc!

    Mohl by jsi mi prosím vysvětlit princip, proč to s get_mut() nefungovalo, a filozofii toho RefCell? Abych to pochopil, co za tím je.
    SPIRALI
    SPIRALI --- ---
    BONEFLUTE: Ah zapomel jsem jeste dodat ze to musis obalit RefCellem, Rc::get_mut ti nebude realne fungovat kdyz se refcounter zvedne.

    Tady je refcell reseni:
    Rust Playground
    https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=30ba12c987875ee3cb76f7b490473098
    BONEFLUTE
    BONEFLUTE --- ---
    Aha :-(

    fn append_to_root(root: &mut Rc<Root>, name: String) {
        Rc::get_mut(root).unwrap().items.push(Item {
    		name: name,
    	});
    }
    BONEFLUTE
    BONEFLUTE --- ---
    SPIRALI:
    Tak jasně. To by snad mělo být: Root::new() -> Rc[Self]. Ale dobře. Chápu tedy, že mi to nebude automaticky předávat jako self: Rc[Root]. OK.

    Podařilo se mi to zjednodušit. Toto funguje:
    struct Item {
        name: String
    }
    
    
    struct Root {
        name: String,
        items: Vec<Item>,
    }
    fn append_to_root(root: &mut Root, name: String) {
        root.items.push(Item { name: name });
    }
    
    
    fn main() {
        let mut root = Root {name: "/".to_string(), items: vec![] };
        append_to_root(&mut root, "Alef".to_string());
    
        println!("C: {}", root.name);
        println!("D: {}", root.items.len());
    }

    Když začnu předávat Rc, a pokud to chápu dobře, tak musím předávat Rc:
    use std::rc::Rc;
    
    
    struct Item {
        name: String
    }
    
    
    struct Root {
        name: String,
        items: Vec<Item>,
    }
    fn append_to_root(root: &mut Rc<Root>, name: String) {
        root.items.push(Item { name: name });
    }
    
    
    fn main() {
        let mut root = Rc::new(Root {name: "/".to_string(), items: vec![] });
        append_to_root(&mut root, "Alef".to_string());
    
        println!("C: {}", root.name);
        println!("D: {}", root.items.len());
    }
    tak to odmítne s:
    error[E0596]: cannot borrow data in an `Rc` as mutable
      --> src/main.rs:14:5
       |
    14 |     root.items.push(Item { name: name });
       |     ^^^^^^^^^^ cannot borrow as mutable

    Omlouvám se, vůbec se nechytám :-(
    SPIRALI
    SPIRALI --- ---
    BONEFLUTE: Ten root musi zit v Rc (Rc::new(Root:new(..)) a append nemuze brat jen self ale musi vzit cele to Rc.
    BONEFLUTE
    BONEFLUTE --- ---
    JUNIOR:
    error[E0308]: mismatched types
      --> src/main.rs:31:29
       |
    31 |         self.items.push(Item::new(&self, itemname));
       |                                   ^^^^^ expected struct `Rc`, found `&mut Root`
       |
       = note: expected reference `&Rc<Root>`
                  found reference `&&mut Root`
    Kliknutím sem můžete změnit nastavení reklam