Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Panic: if `get_resource` is called (provides None if missing), then `init_resource`, get a panic

Aperta
#488 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
30/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
rust
Ambito
backend

Direzione di ricerca

Start by reading the shown get and init_resource implementations, then inspect contains and the resource map behavior to confirm how an empty entry is represented. Update the check so init_resource initializes when the resource has no value, and add or run a regression test for calling get_resource before init_resource to verify it no longer panics.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

world.get_resource does this:

    #[track_caller]
    pub fn get<T: HasSchema>(&self) -> Option<Ref<T>> {
        let b = self.untyped.get(T::schema()).borrow();
        if b.is_some() {
            Some(Ref::map(b, |b| unsafe {
                b.as_ref().unwrap().as_ref().cast_into_unchecked()
            }))
        } else {
            None
        }
    }

self.untyped.get() This inserts the the schema id into OnceMap, with contents being empty.

init_resource does:

    pub fn init_resource<R: HasSchema + FromWorld>(&mut self) -> RefMut<R> {
        if unlikely(!self.resources.contains::<R>()) {
            let value = R::from_world(self);
            self.resources.insert(value);
        }
        self.resource_mut()
    }

and contains only checks if ID is contained - not if resource has a value. This skips the init branch, and only calls self.resource_mut which panics on missing resource value.

This is the only place in bones that contains is used, I think we should update this to check if the resource has a value, and not just ID entry in map. This is what I would expect when using the function as well.

Lingua principale
Rust
Stelle
314
Fork
26
Merge medio
1g 23h
PR unite (30g)
1

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di fishfolk/bones

Tutte le issue di fishfolk/bones

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.