Think about a stack based state, and keeping track of some info you store in a map. Is it possible in Rust (and if so, what would you use) to implement a copy on write strategy for those maps?
On the C++ side I have the following GraphicsState
(the copy on write was implemented after the book's publication):
struct GraphicsState {
...
// Updated after book publication: floatTextures, spectrumTextures, and
// namedMaterials are all implemented using a "copy on write" approach
// for more efficient GraphicsState management. When state is pushed
// in pbrtAttributeBegin(), we don't immediately make a copy of these
// maps, but instead record that each one is shared. Only if an item
// is added to one is a unique copy actually made.
using FloatTextureMap = std::map<std::string, std::shared_ptr<Texture<Float>>>;
std::shared_ptr<FloatTextureMap> floatTextures;
bool floatTexturesShared = false;
using SpectrumTextureMap = std::map<std::string, std::shared_ptr<Texture<Spectrum>>>;
std::shared_ptr<SpectrumTextureMap> spectrumTextures;
bool spectrumTexturesShared = false;
using NamedMaterialMap = std::map<std::string, std::shared_ptr<MaterialInstance>>;
std::shared_ptr<NamedMaterialMap> namedMaterials;
bool namedMaterialsShared = false;
...
}
So far I have used HashMap
for those maps on the Rust side:
#[derive(Default)]
pub struct GraphicsState {
...
pub float_textures: HashMap<String, Arc<Texture<Float> + Send + Sync>>,
pub spectrum_textures: HashMap<String, Arc<Texture<Spectrum> + Send + Sync>>,
...
pub named_materials: HashMap<String, Option<Arc<Material + Send + Sync>>>,
...
}
Any ideas?