douweschulte / pdbtbx

A library to open/edit/save (crystallographic) Protein Data Bank (PDB) and mmCIF files in Rust.

Home Page:https://crates.io/crates/pdbtbx

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MutHierarchies are unsound

douweschulte opened this issue · comments

If you create a hierarchy you could potentially delete the selected lower levels from a higher level. For example you select (Model(0), Chain("A"), Residue(123), Conformer("ALA"), Atom("CA", 450, "C")) and then use chain.remove_atoms_by(|a| a.element() == "C") this would delete the selected atom, and would cause undefined behaviour when the atom is used afterwards.

The proposal is to create a trait with all safe functions for each level and make the levels implement it. The hierarchies can then return a trait object instead of the struct which would limit the use of the unsound functions. The unsound functions are all deleting functions and adding/inserting has to be checked thoroughly for potential issues. Any function that returns mut references to lower levels in the hierarchy also has to have a safe counterpart returning a Trait object.

Unsound:

  • Methods removing items below
  • Methods returning a &mut to an item in any level below?
  • Sort?
  • Extend (and join)?
  • Add lower level item? (what if this triggers a reallocation of the inner Vec?)

It might be solved with the use on Pin