Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Rust programming language

[es] :: Ostali programski jezici :: Rust programming language

Strane: < .. 1 2 3 4

[ Pregleda: 16846 | Odgovora: 64 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

clydefrog

Član broj: 340220
Poruke: 107



+5 Profil

icon Re: Rust programming language01.05.2020. u 21:27 - pre 48 meseci
Bane, hvala sto si se javio.

Ajd' da skratimo primer. Kako proslediti instancu Application-a u UserInterface?

Code:
use std::rc::Rc;
use std::cell::RefCell;

struct Application {
    user_interface: Rc<RefCell<UserInterface>>,
    all_entries: Vec<String>
}

struct UserInterface {
    app: Application,
}

impl Application {
    fn new() -> Self {
        Self {
            user_interface: Rc::new(RefCell::new(UserInterface::new())),
            all_entries: vec!["spam".to_string(), "eggs".to_string()]
        }
    }
}

impl UserInterface {
    fn new() -> Self {
        Self {
            app: ???
        }
    }
}

fn main() {}


[Ovu poruku je menjao clydefrog dana 01.05.2020. u 22:42 GMT+1]
 
Odgovor na temu

Branimir Maksimovic

Član broj: 64947
Poruke: 5534
109.72.51.23



+1064 Profil

icon Re: Rust programming language01.05.2020. u 22:48 - pre 48 meseci
Evo kako ide:
Code:

~/.../examples/rust >>> cat cyclic.rs                                                                                                                                                                    
use std::rc::Rc;
use std::cell::RefCell;

struct Application {
    user_interface: Option<Rc<RefCell<UserInterface>>>,
    all_entries: Vec<String>
}

struct UserInterface {
    app: Rc<RefCell<Application>>,
}
impl Application {
    fn new() -> Rc<RefCell<Self>> {
        let rc = Self {
            user_interface: None,
            all_entries: vec!["spam".to_string(), "eggs".to_string()]
        };
        let rc = Rc::new(RefCell::new(rc));
        rc.borrow_mut().user_interface = Some(Rc::new(RefCell::new(UserInterface::new(rc.clone()))));
        rc.clone()
    }
    fn run(&self) {
        self.user_interface.as_ref().unwrap().borrow().run();
    }
    fn hi(&self) {
        println!("hi!");
    }
}

impl UserInterface {
    fn new(app:Rc<RefCell<Application>>) -> Self {
        Self {
            app: app.clone()
        }
    }
    fn run(&self) {
        self.app.borrow().hi();
    }
}

fn main() {
    let app = Application::new();
    app.borrow().run();
}

Mislim da je ocigledno, ako treba nesto da se objasni, tu sam.
 
Odgovor na temu

clydefrog

Član broj: 340220
Poruke: 107



+5 Profil

icon Re: Rust programming language02.05.2020. u 12:43 - pre 48 meseci
Bane, hvala ti puno.

Nego, nesto mi govori da se ovo ovako ne radi u Rustu i da bi trebalo da zaobidjem ciklicne reference u sirokom luku.

Jel mozes da mi pomognes da struktuiram kod drugacije?

Code:

# app.py


class App:
    def __init__(self):
        self.user_interface = UserInterface(self)
        self.all_entries = []

    def search(self):
        # ovde mi treba pristup self.user_interface
        pass

    def delete_from_history(self, command):
        # ovde mi treba pristup self.user_interface
        pass

    def toggle(self):
        # ovde mi treba pristup self
        pass
    
    def add_to_or_remove_from_favorites(self, command):
           # ovde mi treba pristup self
           pass


def main():
    app = App()
    app.user_interface.populate_screen()

    while True:
        try:
            user_input = app.stdscr.get_wch()
        except curses.error:
            continue

        if user_input == "\x1b": # ESC
            break

# user_interface.py

class UserInterface:
    def __init__(self, app):
        self.app = app
        self.page = Page(self.app)
        self.search_string = ""

    def populate_screen(self):
        # ovde mi treba pristup self.app i page

    def prompt_for_deletion(self, command):
        pass

class EntryCounter:
    def __init__(self, app):
        self.app = app

    def inc(self):
         # ovde mi treba pristup page
         pass

    def dec(self):
         # ovde mi treba pristup page
         pass


class Page:
    def __init__(self, app):
        self.app = app
        self.selected = LineCounter(self.app)

    def inc(self):
         # ovde mi treba pristup self.app
         pass

    def dec(self):
         # ovde mi treba pristup self
         pass

    def total_pages(self):
         # ovde mi treba pristup self.app
         pass

    def get_page_size(self):
         # ovde mi treba pristup self.app
         pass

    def get_page(self):
         # ovde mi treba pristup self.app
         pass
    
    def get_selected(self):
         # ovde mi treba pristup self
         pass



Kako sve ovo organizovati a da ne bude ciklicnih referenci, ili da ih bude minimum ako bas mora?
Inace ovo je suggest box za bash history.
 
Odgovor na temu

Branimir Maksimovic

Član broj: 64947
Poruke: 5534
109.72.51.23



+1064 Profil

icon Re: Rust programming language02.05.2020. u 13:16 - pre 48 meseci
Pa nema tu sta mnogo filozofije, ovde je u principu problematican app koji se svuda provlaci.
Ako neces ciklicne reference, onda potrebne podatke prosledi u parametrima metoda.
Potrebno je znaci da app ima referencu na ui, a ui na page i to je to.
 
Odgovor na temu

Brodoplovac
Beograd

Član broj: 171299
Poruke: 838
91.148.110.*



+166 Profil

icon Re: Rust programming language30.05.2021. u 20:48 - pre 35 meseci
Prikačeni fajlovi
 
Odgovor na temu

[es] :: Ostali programski jezici :: Rust programming language

Strane: < .. 1 2 3 4

[ Pregleda: 16846 | Odgovora: 64 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.