• 2xsaiko@discuss.tchncs.de
    link
    fedilink
    arrow-up
    8
    ·
    10 hours ago

    Do you have some public code you could link to that you’re having this issue with? There isn’t a one-size-fits-all solution for Rc/RefCell, I think.

    • r00ty@kbin.life
      link
      fedilink
      arrow-up
      11
      ·
      edit-2
      10 hours ago

      The current thing I’m working on (processor for iptv m3u files) isn’t public yet, it’s still in the very early stages. Some of the “learning to fly” rust projects I’ve done so far are here though:

      https://git.nerfed.net/r00ty/bingo/_rust (it’s a multi-threaded bingo game simulator, that I made because of the stand-up maths video on the subject).
      https://git.nerfed.net/r00ty/spectrum/_screen (this is a port of part of a general CPU emulation project I did in C#, it emulates the ZX spectrum screen, you can load in the 6912 byte screens and it will show it in a 2x scaled window).

      I think both of these are rather using Arc<RwLock<Thing>> because they both operate in a threaded environment. Bingo is wholly multi-threaded and the spectrum screen is meant to be used by a CPU emulator running in another thread. So not quite the same thing. But you can probably see a lot of jamming the wrong shape in the wrong hole in both of those.

      The current project isn’t multi-threaded. So it has a lot of the Rc/Rc<RefCell> action instead.

      EDIT: Just to give the reason for Rc<RefCell> in the current project. I’m reading in a M3U file and I’m going to be referencing it against an Excel file. So in the structure for the m3u file, I have two BtreeMaps, one for order by channel number and one by name. Each containing references to the same Channel object.

      Likewise the same channel objects are stored in the structure for the Excel file that is read in (searched for in the m3u file structure).

      BTreeMaps used because in different scenarios the contents will be output in either name order or channel order. So just better to put them in, in that order in the first place.

      • 2xsaiko@discuss.tchncs.de
        link
        fedilink
        arrow-up
        1
        ·
        2 hours ago

        The bingo one actually uses crossbeam channels instead of mutexes, so that’s nice. I haven’t looked too closely at it though.

        I don’t think you can do too much about the Spectrum one if you want to keep the two threads, but here’s what I would change related to thread synchronization. Lemmy doesn’t seem to allow me to attach patch files for whatever reason so have an archive instead… https://dblsaiko.net/pub/tmp/patches.tar.bz2 (I wrote a few notes in the commit messages)

        Just to give the reason for Rc<RefCell> in the current project. I’m reading in a M3U file and I’m going to be referencing it against an Excel file. So in the structure for the m3u file, I have two BtreeMaps, one for order by channel number and one by name. Each containing references to the same Channel object.

        So basically it’s channels indexed by channel number and name? That one is actually one of the easy cases. Store indices instead:

        struct Channels {
          data: Vec<Channel>,
          by_number: BTreeMap<u32 /* or whatever */, usize>,
          by_name: BTreeMap<String, usize>,
        }
        
        // untested but I think it should compile
        fn get_channel_by_name(ch: &Channels, name: &str) -> Option<&Channel> {
          Some(&self.data[*ch.by_name.get(name)?])
        }