Skip to content

Commit 50a16a3

Browse files
committed
Simplify accessor and modifier functions
1 parent 9512b88 commit 50a16a3

File tree

2 files changed

+23
-50
lines changed

2 files changed

+23
-50
lines changed

rust/kcl-lib/src/execution/mod.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,13 @@ impl ExecOutcome {
195195
pub fn scene_object_by_id(&self, id: ObjectId) -> Option<&Object> {
196196
#[cfg(feature = "artifact-graph")]
197197
{
198-
if let Some(first_object) = self.scene_objects.first() {
199-
// The scene objects are stored in order, but they may start
200-
// with a non-zero ID. If so, shift the ID down to get the
201-
// index.
202-
debug_assert!(id.0 >= first_object.id.0);
203-
let index = id.0.checked_sub(first_object.id.0);
204-
index.and_then(|i| self.scene_objects.get(i))
205-
} else {
206-
None
207-
}
198+
debug_assert!(
199+
id.0 < self.scene_objects.len(),
200+
"Requested object ID {} but only have {} objects",
201+
id.0,
202+
self.scene_objects.len()
203+
);
204+
self.scene_objects.get(id.0)
208205
}
209206
#[cfg(not(feature = "artifact-graph"))]
210207
{

rust/kcl-lib/src/execution/state.rs

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ impl ExecState {
383383
/// Update a scene object. This is useful to replace a placeholder.
384384
#[cfg(feature = "artifact-graph")]
385385
pub fn set_scene_object(&mut self, object: Object) {
386-
self.mod_local.artifacts.set_scene_object(object);
386+
let id = object.id;
387+
self.mod_local.artifacts.scene_objects[id.0] = object;
387388
}
388389

389390
#[cfg(feature = "artifact-graph")]
@@ -730,16 +731,13 @@ impl ModuleArtifactState {
730731
pub(crate) fn scene_object_by_id(&self, id: ObjectId) -> Option<&Object> {
731732
#[cfg(feature = "artifact-graph")]
732733
{
733-
if let Some(first_object) = self.scene_objects.first() {
734-
// The scene objects are stored in order, but they may start
735-
// with a non-zero ID. If so, shift the ID down to get the
736-
// index.
737-
debug_assert!(id.0 >= first_object.id.0);
738-
let index = id.0.checked_sub(first_object.id.0);
739-
index.and_then(|i| self.scene_objects.get(i))
740-
} else {
741-
None
742-
}
734+
debug_assert!(
735+
id.0 < self.scene_objects.len(),
736+
"Requested object ID {} but only have {} objects",
737+
id.0,
738+
self.scene_objects.len()
739+
);
740+
self.scene_objects.get(id.0)
743741
}
744742
#[cfg(not(feature = "artifact-graph"))]
745743
{
@@ -752,42 +750,20 @@ impl ModuleArtifactState {
752750
pub(crate) fn scene_object_by_id_mut(&mut self, id: ObjectId) -> Option<&mut Object> {
753751
#[cfg(feature = "artifact-graph")]
754752
{
755-
if let Some(first_object) = self.scene_objects.first() {
756-
// The scene objects are stored in order, but they may start
757-
// with a non-zero ID. If so, shift the ID down to get the
758-
// index.
759-
debug_assert!(id.0 >= first_object.id.0);
760-
let index = id.0.checked_sub(first_object.id.0);
761-
index.and_then(|i| self.scene_objects.get_mut(i))
762-
} else {
763-
None
764-
}
753+
debug_assert!(
754+
id.0 < self.scene_objects.len(),
755+
"Requested object ID {} but only have {} objects",
756+
id.0,
757+
self.scene_objects.len()
758+
);
759+
self.scene_objects.get_mut(id.0)
765760
}
766761
#[cfg(not(feature = "artifact-graph"))]
767762
{
768763
let _ = id;
769764
None
770765
}
771766
}
772-
773-
#[cfg_attr(not(feature = "artifact-graph"), expect(dead_code))]
774-
pub(crate) fn set_scene_object(&mut self, object: Object) {
775-
#[cfg(feature = "artifact-graph")]
776-
{
777-
if let Some(first_object) = self.scene_objects.first() {
778-
// The scene objects are stored in order, but they may start
779-
// with a non-zero ID. If so, shift the ID down to get the
780-
// index.
781-
debug_assert!(object.id.0 >= first_object.id.0);
782-
let index = object.id.0.checked_sub(first_object.id.0);
783-
if let Some(index) = index {
784-
self.scene_objects[index] = object;
785-
}
786-
}
787-
}
788-
#[cfg(not(feature = "artifact-graph"))]
789-
drop(object);
790-
}
791767
}
792768

793769
impl ModuleState {

0 commit comments

Comments
 (0)