From 6a845aec5709e0e893b2fe59f8fa2e48e9f614f1 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Mar 2023 14:35:51 +0100 Subject: [PATCH 01/11] Remove unused builder traits --- crates/fj-kernel/src/builder/face.rs | 9 --------- crates/fj-kernel/src/builder/mod.rs | 9 +-------- crates/fj-kernel/src/builder/shell.rs | 9 --------- crates/fj-kernel/src/builder/sketch.rs | 9 --------- crates/fj-kernel/src/builder/solid.rs | 9 --------- 5 files changed, 1 insertion(+), 44 deletions(-) delete mode 100644 crates/fj-kernel/src/builder/face.rs delete mode 100644 crates/fj-kernel/src/builder/shell.rs delete mode 100644 crates/fj-kernel/src/builder/sketch.rs delete mode 100644 crates/fj-kernel/src/builder/solid.rs diff --git a/crates/fj-kernel/src/builder/face.rs b/crates/fj-kernel/src/builder/face.rs deleted file mode 100644 index ca70ce008..000000000 --- a/crates/fj-kernel/src/builder/face.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::objects::Face; - -/// Builder API for [`Face`] -pub trait FaceBuilder { - // No methods are currently defined. This trait serves as a placeholder, to - // make it clear where to add such methods, once necessary. -} - -impl FaceBuilder for Face {} diff --git a/crates/fj-kernel/src/builder/mod.rs b/crates/fj-kernel/src/builder/mod.rs index 5447b2d14..bef39d519 100644 --- a/crates/fj-kernel/src/builder/mod.rs +++ b/crates/fj-kernel/src/builder/mod.rs @@ -3,17 +3,10 @@ // These are new-style builders that build on top of the partial object API. mod cycle; mod edge; -mod face; -mod shell; -mod sketch; -mod solid; use std::array; -pub use self::{ - cycle::CycleBuilder, edge::HalfEdgeBuilder, face::FaceBuilder, - shell::ShellBuilder, sketch::SketchBuilder, solid::SolidBuilder, -}; +pub use self::{cycle::CycleBuilder, edge::HalfEdgeBuilder}; /// Pass objects to a builder method /// diff --git a/crates/fj-kernel/src/builder/shell.rs b/crates/fj-kernel/src/builder/shell.rs deleted file mode 100644 index 233cc3e81..000000000 --- a/crates/fj-kernel/src/builder/shell.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::partial::PartialShell; - -/// Builder API for [`PartialShell`] -pub trait ShellBuilder { - // No methods are currently defined. This trait serves as a placeholder, to - // make it clear where to add such methods, once necessary. -} - -impl ShellBuilder for PartialShell {} diff --git a/crates/fj-kernel/src/builder/sketch.rs b/crates/fj-kernel/src/builder/sketch.rs deleted file mode 100644 index 2a1ccfbd2..000000000 --- a/crates/fj-kernel/src/builder/sketch.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::partial::PartialSketch; - -/// Builder API for [`PartialSketch`] -pub trait SketchBuilder { - // No methods are currently defined. This trait serves as a placeholder, to - // make it clear where to add such methods, once necessary. -} - -impl SketchBuilder for PartialSketch {} diff --git a/crates/fj-kernel/src/builder/solid.rs b/crates/fj-kernel/src/builder/solid.rs deleted file mode 100644 index 9053f0c05..000000000 --- a/crates/fj-kernel/src/builder/solid.rs +++ /dev/null @@ -1,9 +0,0 @@ -use crate::partial::PartialSolid; - -/// Builder API for [`PartialSolid`] -pub trait SolidBuilder { - // No methods are currently defined. This trait serves as a placeholder, to - // make it clear where to add such methods, once necessary. -} - -impl SolidBuilder for PartialSolid {} From 1f48b9ca9e20da9adc1d4617bc827005d16e6772 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Mar 2023 14:36:58 +0100 Subject: [PATCH 02/11] Replace use of `PartialSketch` --- crates/fj-operations/src/difference_2d.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/fj-operations/src/difference_2d.rs b/crates/fj-operations/src/difference_2d.rs index 45cdc37b6..758dd5210 100644 --- a/crates/fj-operations/src/difference_2d.rs +++ b/crates/fj-operations/src/difference_2d.rs @@ -5,7 +5,6 @@ use fj_kernel::{ algorithms::reverse::Reverse, insert::Insert, objects::{Face, Objects, Sketch}, - partial::{PartialObject, PartialSketch}, services::Service, }; use fj_math::Aabb; @@ -87,7 +86,7 @@ impl Shape for fj::Difference2d { faces.push(face.insert(objects)); } - let difference = PartialSketch { faces }.build(objects).insert(objects); + let difference = Sketch::new(faces).insert(objects); difference.deref().clone() } From 00840e761eda6b50155c276b329d5d384cb61ff0 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Mar 2023 14:37:57 +0100 Subject: [PATCH 03/11] Replace use of `PartialSketch` --- crates/fj-operations/src/sketch.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/crates/fj-operations/src/sketch.rs b/crates/fj-operations/src/sketch.rs index 967b0d76c..5246fdc09 100644 --- a/crates/fj-operations/src/sketch.rs +++ b/crates/fj-operations/src/sketch.rs @@ -5,7 +5,6 @@ use fj_kernel::{ builder::{CycleBuilder, HalfEdgeBuilder}, insert::Insert, objects::{Cycle, Face, Objects, Sketch}, - partial::{PartialObject, PartialSketch}, services::Service, }; use fj_math::{Aabb, Point}; @@ -85,11 +84,7 @@ impl Shape for fj::Sketch { } }; - let sketch = PartialSketch { - faces: vec![face.insert(objects)], - } - .build(objects) - .insert(objects); + let sketch = Sketch::new(vec![face.insert(objects)]).insert(objects); sketch.deref().clone() } From a1e145e0bfd43f21f40b3c83261289ae8bd5879f Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Mar 2023 14:39:02 +0100 Subject: [PATCH 04/11] Remove `PartialSketch` --- crates/fj-kernel/src/partial/mod.rs | 4 +-- crates/fj-kernel/src/partial/objects/mod.rs | 1 - .../fj-kernel/src/partial/objects/sketch.rs | 31 ------------------- crates/fj-kernel/src/partial/traits.rs | 1 - 4 files changed, 1 insertion(+), 36 deletions(-) delete mode 100644 crates/fj-kernel/src/partial/objects/sketch.rs diff --git a/crates/fj-kernel/src/partial/mod.rs b/crates/fj-kernel/src/partial/mod.rs index abe19bd5f..398e36a02 100644 --- a/crates/fj-kernel/src/partial/mod.rs +++ b/crates/fj-kernel/src/partial/mod.rs @@ -15,9 +15,7 @@ mod traits; mod wrapper; pub use self::{ - objects::{ - shell::PartialShell, sketch::PartialSketch, solid::PartialSolid, - }, + objects::{shell::PartialShell, solid::PartialSolid}, traits::{HasPartial, PartialObject}, wrapper::{FullToPartialCache, Partial}, }; diff --git a/crates/fj-kernel/src/partial/objects/mod.rs b/crates/fj-kernel/src/partial/objects/mod.rs index 24dc72ba8..a86c51a25 100644 --- a/crates/fj-kernel/src/partial/objects/mod.rs +++ b/crates/fj-kernel/src/partial/objects/mod.rs @@ -1,3 +1,2 @@ pub mod shell; -pub mod sketch; pub mod solid; diff --git a/crates/fj-kernel/src/partial/objects/sketch.rs b/crates/fj-kernel/src/partial/objects/sketch.rs deleted file mode 100644 index f6e612697..000000000 --- a/crates/fj-kernel/src/partial/objects/sketch.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::{ - objects::{Face, Objects, Sketch}, - partial::{FullToPartialCache, PartialObject}, - services::Service, - storage::Handle, -}; - -/// A partial [`Sketch`] -#[derive(Clone, Debug)] -pub struct PartialSketch { - /// The faces that make up the sketch - pub faces: Vec>, -} - -impl PartialObject for PartialSketch { - type Full = Sketch; - - fn new(_: &mut Service) -> Self { - Self { faces: Vec::new() } - } - - fn from_full(sketch: &Self::Full, _: &mut FullToPartialCache) -> Self { - Self { - faces: sketch.faces().into_iter().cloned().collect(), - } - } - - fn build(self, _: &mut Service) -> Self::Full { - Sketch::new(self.faces) - } -} diff --git a/crates/fj-kernel/src/partial/traits.rs b/crates/fj-kernel/src/partial/traits.rs index 61e8b4272..215621768 100644 --- a/crates/fj-kernel/src/partial/traits.rs +++ b/crates/fj-kernel/src/partial/traits.rs @@ -37,6 +37,5 @@ macro_rules! impl_trait { impl_trait!( Shell, PartialShell; - Sketch, PartialSketch; Solid, PartialSolid; ); From d361ddff360457162735a40d5b0afe4c3b96d82a Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Mar 2023 14:40:21 +0100 Subject: [PATCH 05/11] Replace use of `PartialShell` --- crates/fj-kernel/src/algorithms/sweep/face.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/sweep/face.rs b/crates/fj-kernel/src/algorithms/sweep/face.rs index 1145c410a..00e7fbfe0 100644 --- a/crates/fj-kernel/src/algorithms/sweep/face.rs +++ b/crates/fj-kernel/src/algorithms/sweep/face.rs @@ -9,7 +9,6 @@ use crate::{ geometry::curve::GlobalPath, insert::Insert, objects::{Cycle, Face, Objects, Shell}, - partial::{PartialObject, PartialShell}, services::Service, storage::Handle, }; @@ -99,6 +98,6 @@ impl Sweep for Handle { let top_face = top_face.insert(objects); faces.push(top_face); - PartialShell { faces }.build(objects).insert(objects) + Shell::new(faces).insert(objects) } } From 9f7b6cebbf39b36dbde034f563a62e1b1f14b9d4 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Mar 2023 14:42:27 +0100 Subject: [PATCH 06/11] Use `Handle` in `PartialSolid` --- crates/fj-kernel/src/algorithms/sweep/sketch.rs | 3 +-- crates/fj-kernel/src/partial/objects/solid.rs | 16 +++++++--------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/sweep/sketch.rs b/crates/fj-kernel/src/algorithms/sweep/sketch.rs index e77bca1b1..17351b99a 100644 --- a/crates/fj-kernel/src/algorithms/sweep/sketch.rs +++ b/crates/fj-kernel/src/algorithms/sweep/sketch.rs @@ -3,7 +3,7 @@ use fj_math::Vector; use crate::{ insert::Insert, objects::{Objects, Sketch, Solid}, - partial::{Partial, PartialObject, PartialSolid}, + partial::{PartialObject, PartialSolid}, services::Service, storage::Handle, }; @@ -27,7 +27,6 @@ impl Sweep for Handle { shells.push(shell); } - let shells = shells.into_iter().map(Partial::from).collect(); PartialSolid { shells }.build(objects).insert(objects) } } diff --git a/crates/fj-kernel/src/partial/objects/solid.rs b/crates/fj-kernel/src/partial/objects/solid.rs index 6ab762ccb..d47b212a0 100644 --- a/crates/fj-kernel/src/partial/objects/solid.rs +++ b/crates/fj-kernel/src/partial/objects/solid.rs @@ -1,14 +1,15 @@ use crate::{ objects::{Objects, Shell, Solid}, - partial::{FullToPartialCache, Partial, PartialObject}, + partial::{FullToPartialCache, PartialObject}, services::Service, + storage::Handle, }; /// A partial [`Solid`] #[derive(Clone, Debug)] pub struct PartialSolid { /// The shells that make up the solid - pub shells: Vec>, + pub shells: Vec>, } impl PartialObject for PartialSolid { @@ -18,17 +19,14 @@ impl PartialObject for PartialSolid { Self { shells: Vec::new() } } - fn from_full(solid: &Self::Full, cache: &mut FullToPartialCache) -> Self { + fn from_full(solid: &Self::Full, _: &mut FullToPartialCache) -> Self { Self { - shells: solid - .shells() - .map(|shell| Partial::from_full(shell.clone(), cache)) - .collect(), + shells: solid.shells().cloned().collect(), } } - fn build(self, objects: &mut Service) -> Self::Full { - let shells = self.shells.into_iter().map(|shell| shell.build(objects)); + fn build(self, _: &mut Service) -> Self::Full { + let shells = self.shells; Solid::new(shells) } } From 078a3ec096a2febdbb63d7dac7e06201dd223fb6 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Mar 2023 14:43:01 +0100 Subject: [PATCH 07/11] Inline redundant variable --- crates/fj-kernel/src/partial/objects/solid.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/fj-kernel/src/partial/objects/solid.rs b/crates/fj-kernel/src/partial/objects/solid.rs index d47b212a0..2b6a99e7b 100644 --- a/crates/fj-kernel/src/partial/objects/solid.rs +++ b/crates/fj-kernel/src/partial/objects/solid.rs @@ -26,7 +26,6 @@ impl PartialObject for PartialSolid { } fn build(self, _: &mut Service) -> Self::Full { - let shells = self.shells; - Solid::new(shells) + Solid::new(self.shells) } } From 2fb19c3da985c624834aaf4cc71bf1b352ca3029 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Mar 2023 14:49:34 +0100 Subject: [PATCH 08/11] Remove `PartialShell` --- crates/fj-kernel/src/partial/mod.rs | 2 +- crates/fj-kernel/src/partial/objects/mod.rs | 1 - crates/fj-kernel/src/partial/objects/shell.rs | 31 ------------------- crates/fj-kernel/src/partial/traits.rs | 1 - 4 files changed, 1 insertion(+), 34 deletions(-) delete mode 100644 crates/fj-kernel/src/partial/objects/shell.rs diff --git a/crates/fj-kernel/src/partial/mod.rs b/crates/fj-kernel/src/partial/mod.rs index 398e36a02..fe5005628 100644 --- a/crates/fj-kernel/src/partial/mod.rs +++ b/crates/fj-kernel/src/partial/mod.rs @@ -15,7 +15,7 @@ mod traits; mod wrapper; pub use self::{ - objects::{shell::PartialShell, solid::PartialSolid}, + objects::solid::PartialSolid, traits::{HasPartial, PartialObject}, wrapper::{FullToPartialCache, Partial}, }; diff --git a/crates/fj-kernel/src/partial/objects/mod.rs b/crates/fj-kernel/src/partial/objects/mod.rs index a86c51a25..742f0af4e 100644 --- a/crates/fj-kernel/src/partial/objects/mod.rs +++ b/crates/fj-kernel/src/partial/objects/mod.rs @@ -1,2 +1 @@ -pub mod shell; pub mod solid; diff --git a/crates/fj-kernel/src/partial/objects/shell.rs b/crates/fj-kernel/src/partial/objects/shell.rs deleted file mode 100644 index 385fd6206..000000000 --- a/crates/fj-kernel/src/partial/objects/shell.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::{ - objects::{Face, Objects, Shell}, - partial::{FullToPartialCache, PartialObject}, - services::Service, - storage::Handle, -}; - -/// A partial [`Shell`] -#[derive(Clone, Debug)] -pub struct PartialShell { - /// The faces that make up the shell - pub faces: Vec>, -} - -impl PartialObject for PartialShell { - type Full = Shell; - - fn new(_: &mut Service) -> Self { - Self { faces: Vec::new() } - } - - fn from_full(shell: &Self::Full, _: &mut FullToPartialCache) -> Self { - Self { - faces: shell.faces().into_iter().cloned().collect(), - } - } - - fn build(self, _: &mut Service) -> Self::Full { - Shell::new(self.faces) - } -} diff --git a/crates/fj-kernel/src/partial/traits.rs b/crates/fj-kernel/src/partial/traits.rs index 215621768..6b7e6b63b 100644 --- a/crates/fj-kernel/src/partial/traits.rs +++ b/crates/fj-kernel/src/partial/traits.rs @@ -36,6 +36,5 @@ macro_rules! impl_trait { } impl_trait!( - Shell, PartialShell; Solid, PartialSolid; ); From 5f31e1055a4b0831d980cdb90e208099c42d7a60 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Mar 2023 14:50:40 +0100 Subject: [PATCH 09/11] Replace use of `PartialSolid` --- crates/fj-kernel/src/algorithms/sweep/sketch.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/sweep/sketch.rs b/crates/fj-kernel/src/algorithms/sweep/sketch.rs index 17351b99a..53170a768 100644 --- a/crates/fj-kernel/src/algorithms/sweep/sketch.rs +++ b/crates/fj-kernel/src/algorithms/sweep/sketch.rs @@ -3,7 +3,6 @@ use fj_math::Vector; use crate::{ insert::Insert, objects::{Objects, Sketch, Solid}, - partial::{PartialObject, PartialSolid}, services::Service, storage::Handle, }; @@ -27,6 +26,6 @@ impl Sweep for Handle { shells.push(shell); } - PartialSolid { shells }.build(objects).insert(objects) + Solid::new(shells).insert(objects) } } From 44eba757080d4577f12fad20e977b31b62895801 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Mar 2023 14:51:57 +0100 Subject: [PATCH 10/11] Remove `PartialSolid` --- crates/fj-kernel/src/partial/mod.rs | 1 - crates/fj-kernel/src/partial/objects/mod.rs | 2 +- crates/fj-kernel/src/partial/objects/solid.rs | 31 ------------------- crates/fj-kernel/src/partial/traits.rs | 4 +-- 4 files changed, 2 insertions(+), 36 deletions(-) delete mode 100644 crates/fj-kernel/src/partial/objects/solid.rs diff --git a/crates/fj-kernel/src/partial/mod.rs b/crates/fj-kernel/src/partial/mod.rs index fe5005628..12fbfab22 100644 --- a/crates/fj-kernel/src/partial/mod.rs +++ b/crates/fj-kernel/src/partial/mod.rs @@ -15,7 +15,6 @@ mod traits; mod wrapper; pub use self::{ - objects::solid::PartialSolid, traits::{HasPartial, PartialObject}, wrapper::{FullToPartialCache, Partial}, }; diff --git a/crates/fj-kernel/src/partial/objects/mod.rs b/crates/fj-kernel/src/partial/objects/mod.rs index 742f0af4e..8b1378917 100644 --- a/crates/fj-kernel/src/partial/objects/mod.rs +++ b/crates/fj-kernel/src/partial/objects/mod.rs @@ -1 +1 @@ -pub mod solid; + diff --git a/crates/fj-kernel/src/partial/objects/solid.rs b/crates/fj-kernel/src/partial/objects/solid.rs deleted file mode 100644 index 2b6a99e7b..000000000 --- a/crates/fj-kernel/src/partial/objects/solid.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::{ - objects::{Objects, Shell, Solid}, - partial::{FullToPartialCache, PartialObject}, - services::Service, - storage::Handle, -}; - -/// A partial [`Solid`] -#[derive(Clone, Debug)] -pub struct PartialSolid { - /// The shells that make up the solid - pub shells: Vec>, -} - -impl PartialObject for PartialSolid { - type Full = Solid; - - fn new(_: &mut Service) -> Self { - Self { shells: Vec::new() } - } - - fn from_full(solid: &Self::Full, _: &mut FullToPartialCache) -> Self { - Self { - shells: solid.shells().cloned().collect(), - } - } - - fn build(self, _: &mut Service) -> Self::Full { - Solid::new(self.shells) - } -} diff --git a/crates/fj-kernel/src/partial/traits.rs b/crates/fj-kernel/src/partial/traits.rs index 6b7e6b63b..683b2562f 100644 --- a/crates/fj-kernel/src/partial/traits.rs +++ b/crates/fj-kernel/src/partial/traits.rs @@ -35,6 +35,4 @@ macro_rules! impl_trait { }; } -impl_trait!( - Solid, PartialSolid; -); +impl_trait!(); From b828bfcdb5d1ab15ee31ab71a87f3471079f26d5 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 15 Mar 2023 14:52:47 +0100 Subject: [PATCH 11/11] Remove rest of partial object API --- crates/fj-kernel/src/lib.rs | 1 - crates/fj-kernel/src/partial/mod.rs | 50 ----- crates/fj-kernel/src/partial/objects/mod.rs | 1 - crates/fj-kernel/src/partial/traits.rs | 38 ---- crates/fj-kernel/src/partial/wrapper.rs | 222 -------------------- 5 files changed, 312 deletions(-) delete mode 100644 crates/fj-kernel/src/partial/mod.rs delete mode 100644 crates/fj-kernel/src/partial/objects/mod.rs delete mode 100644 crates/fj-kernel/src/partial/traits.rs delete mode 100644 crates/fj-kernel/src/partial/wrapper.rs diff --git a/crates/fj-kernel/src/lib.rs b/crates/fj-kernel/src/lib.rs index 6a8757f2f..9f035bbc6 100644 --- a/crates/fj-kernel/src/lib.rs +++ b/crates/fj-kernel/src/lib.rs @@ -92,7 +92,6 @@ pub mod builder; pub mod geometry; pub mod insert; pub mod objects; -pub mod partial; pub mod services; pub mod storage; pub mod validate; diff --git a/crates/fj-kernel/src/partial/mod.rs b/crates/fj-kernel/src/partial/mod.rs deleted file mode 100644 index 12fbfab22..000000000 --- a/crates/fj-kernel/src/partial/mod.rs +++ /dev/null @@ -1,50 +0,0 @@ -//! Partially defined objects -//! -//! This module contains types that mirror the full object types from -//! [`crate::objects`], only the types from this module can be defined only -//! partially, with the non-defined parts being inferred when a full object is -//! constructed. -//! -//! # Implementation Note -//! -//! This API was created as a replacement for the [original partial object -//! API][crate::partial]. This is still a work in progress. - -mod objects; -mod traits; -mod wrapper; - -pub use self::{ - traits::{HasPartial, PartialObject}, - wrapper::{FullToPartialCache, Partial}, -}; - -use crate::storage::Handle; - -/// Either a full or a partial object -/// -/// # Implementation Note -/// -/// This enum temporarily exists to aid in the transition towards a unified -/// object system. Issue: -/// -#[derive(Clone, Debug)] -pub enum FullOrPartial { - /// A full object - Full(Handle), - - /// A partial object - Partial(Partial), -} - -impl From> for FullOrPartial { - fn from(object: Handle) -> Self { - Self::Full(object) - } -} - -impl From> for FullOrPartial { - fn from(object: Partial) -> Self { - Self::Partial(object) - } -} diff --git a/crates/fj-kernel/src/partial/objects/mod.rs b/crates/fj-kernel/src/partial/objects/mod.rs deleted file mode 100644 index 8b1378917..000000000 --- a/crates/fj-kernel/src/partial/objects/mod.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/crates/fj-kernel/src/partial/traits.rs b/crates/fj-kernel/src/partial/traits.rs deleted file mode 100644 index 683b2562f..000000000 --- a/crates/fj-kernel/src/partial/traits.rs +++ /dev/null @@ -1,38 +0,0 @@ -use std::fmt::Debug; - -use crate::{objects::Objects, services::Service}; - -use super::FullToPartialCache; - -/// Implemented for objects that a partial object variant exists for -pub trait HasPartial { - /// The type representing the partial variant of this object - type Partial: PartialObject; -} - -/// Implemented for partial objects -pub trait PartialObject: Clone + Debug { - /// The type representing the full object - type Full: HasPartial; - - /// Construct a default partial object - fn new(objects: &mut Service) -> Self; - - /// Construct a partial object from a full one - fn from_full(full: &Self::Full, cache: &mut FullToPartialCache) -> Self; - - /// Build a full object from the partial object - fn build(self, objects: &mut Service) -> Self::Full; -} - -macro_rules! impl_trait { - ($($full:ident, $partial:ident;)*) => { - $( - impl HasPartial for crate::objects::$full { - type Partial = super::$partial; - } - )* - }; -} - -impl_trait!(); diff --git a/crates/fj-kernel/src/partial/wrapper.rs b/crates/fj-kernel/src/partial/wrapper.rs deleted file mode 100644 index d42353bda..000000000 --- a/crates/fj-kernel/src/partial/wrapper.rs +++ /dev/null @@ -1,222 +0,0 @@ -use std::{ - any::type_name, - collections::BTreeMap, - fmt::{self, Debug}, - ops::{Deref, DerefMut}, - sync::Arc, -}; - -use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard}; -use type_map::TypeMap; - -use crate::{ - insert::Insert, - objects::Objects, - partial::traits::PartialObject, - services::Service, - storage::{Handle, ObjectId}, -}; - -use super::HasPartial; - -/// Wrapper around a partial object -/// -/// Controls access to the partial object. Can be cloned, to access the same -/// partial object from multiple locations. -pub struct Partial { - inner: Inner, -} - -impl Partial { - /// Construct a `Partial` with a default inner partial object - pub fn new(objects: &mut Service) -> Self { - Self::from_partial(T::Partial::new(objects)) - } - - /// Construct a `Partial` from a partial object - pub fn from_partial(partial: T::Partial) -> Self { - let inner = Inner::new(InnerObject { - partial, - full: None, - }); - Self { inner } - } - - /// Construct a partial from a full object - pub fn from_full(full: Handle, cache: &mut FullToPartialCache) -> Self { - let inner = match cache.get(&full) { - Some(inner) => inner, - None => { - let inner = Inner::new(InnerObject { - partial: T::Partial::from_full(&full, cache), - full: Some(full.clone()), - }); - - cache.insert(&full, inner.clone()); - - inner - } - }; - - Self { inner } - } - - /// Access the ID of this partial object - pub fn id(&self) -> ObjectId { - self.inner.id() - } - - /// Access the partial object - pub fn read(&self) -> impl Deref + '_ { - RwLockReadGuard::map(self.inner.read(), |inner| &inner.partial) - } - - /// Access the partial object mutably - /// - /// # Panics - /// - /// Panics, if this method is called while the return value from a previous - /// call to this method of [`Self::read`] is still borrowed. - pub fn write(&mut self) -> impl DerefMut + '_ { - let mut inner = self.inner.write(); - - // If we created this partial object from a full one and then modify it, - // it should not map back to the full object when calling `build`. - inner.full = None; - - RwLockWriteGuard::map(inner, |inner| &mut inner.partial) - } - - /// Build a full object from this partial one - /// - /// # Panics - /// - /// Panics, if a call to [`Self::write`] would panic. - pub fn build(self, objects: &mut Service) -> Handle - where - T: Insert, - { - let mut inner = self.inner.write(); - - // If another instance of this `Partial` has already been built, re-use - // the resulting full object. - let partial = inner.partial.clone(); - let full = inner - .full - .get_or_insert_with(|| partial.build(objects).insert(objects)); - - full.clone() - } -} - -impl fmt::Debug for Partial -where - T::Partial: Debug, -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let name = { - let type_name = type_name::(); - match type_name.rsplit_once("::") { - Some((_, name)) => name, - None => type_name, - } - }; - let id = self.id().0; - let object = self.read().clone(); - - if f.alternate() { - write!(f, "{name} @ {id:#x} => {object:#?}")?; - } else { - write!(f, "{name} @ {id:#x}")?; - } - - Ok(()) - } -} - -impl Clone for Partial { - fn clone(&self) -> Self { - Self { - inner: self.inner.clone(), - } - } -} - -impl From> for Partial { - fn from(full: Handle) -> Self { - let mut cache = FullToPartialCache::default(); - Self::from_full(full, &mut cache) - } -} - -#[derive(Debug)] -struct Inner(Arc>>); - -impl Inner { - fn new(inner: InnerObject) -> Self { - Self(Arc::new(RwLock::new(inner))) - } - - fn id(&self) -> ObjectId { - ObjectId::from_ptr(Arc::as_ptr(&self.0)) - } - - fn read(&self) -> RwLockReadGuard> { - self.0 - .try_read() - .expect("Tried to read `Partial` that is currently being modified") - } - - fn write(&self) -> RwLockWriteGuard> { - self.0 - .try_write() - .expect("Tried to modify `Partial` that is currently locked") - } -} - -impl Clone for Inner { - fn clone(&self) -> Self { - Self(self.0.clone()) - } -} - -#[derive(Debug)] -struct InnerObject { - partial: T::Partial, - full: Option>, -} - -/// Caches conversions from full to partial objects -/// -/// When creating a whole graph of partial objects from a graph of full ones, -/// the conversions must be cached, to ensure that each full object maps to -/// exactly one partial object. -/// -/// Used by [`Partial::from_full`] and [`PartialObject::from_full`]. -#[derive(Default)] -pub struct FullToPartialCache(TypeMap); - -impl FullToPartialCache { - fn get(&mut self, handle: &Handle) -> Option> - where - T: HasPartial + 'static, - { - self.map().get(&handle.id()).cloned() - } - - fn insert(&mut self, handle: &Handle, inner: Inner) - where - T: HasPartial + 'static, - { - self.map().insert(handle.id(), inner); - } - - fn map(&mut self) -> &mut BTreeMap> - where - T: HasPartial + 'static, - { - self.0 - .entry::>>() - .or_insert_with(BTreeMap::new) - } -}