Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up objects service code #1715

Merged
merged 9 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ macro_rules! impl_insert {
fn insert(self, objects: &mut Service<Objects>) -> Handle<Self>
{
let handle = objects.$store.reserve();
objects.insert(handle.clone(), self);
objects.insert((handle.clone(), self).into());
handle
}
}
Expand Down
19 changes: 0 additions & 19 deletions crates/fj-kernel/src/objects/object.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::any::Any;

use crate::{
objects::{
Cycle, Face, GlobalEdge, HalfEdge, Objects, Shell, Sketch, Solid,
Expand All @@ -24,23 +22,6 @@ macro_rules! object {
)*
}

impl<F: Form> Object<F> {
/// Convert the `Object` into the requested inner type
pub fn as_inner<T>(&self) -> Option<&F::Form<T>>
where
Self: 'static,
F::Form<T>: Any,
{
match self {
$(
Self::$ty(object) =>
(object as &dyn Any).downcast_ref(),

)*
}
}
}

impl Object<BehindHandle> {
/// Access the ID of the object
pub fn id(&self) -> ObjectId {
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use parking_lot::Mutex;
use crate::objects::Objects;

pub use self::{
objects::ServiceObjectsExt,
objects::{InsertObject, Operation, ServiceObjectsExt},
service::{Service, State},
validation::{Validation, ValidationFailed},
};
Expand Down
50 changes: 22 additions & 28 deletions crates/fj-kernel/src/services/objects.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
use crate::{
objects::{Object, Objects, WithHandle},
storage::Handle,
};
use crate::objects::{Object, Objects, WithHandle};

use super::{Service, State};

impl State for Objects {
type Command = InsertObject;
type Event = ObjectToInsert;
type Command = Operation;
type Event = InsertObject;

fn decide(&self, command: Self::Command, events: &mut Vec<Self::Event>) {
let event = ObjectToInsert {
object: command.object,
};
events.push(event);
let Operation::InsertObject { object } = command;
events.push(InsertObject { object });
}

fn evolve(&mut self, event: &Self::Event) {
Expand All @@ -22,37 +17,36 @@ impl State for Objects {
}

/// Command for `Service<Objects>`
///
/// You might prefer to use [`ServiceObjectsExt::insert`], which is a convenient
/// wrapper around `Service<Objects>::execute`.
#[derive(Clone, Debug)]
pub struct InsertObject {
/// The object to insert
pub object: Object<WithHandle>,
#[derive(Debug)]
pub enum Operation {
/// Insert an object into the stores
///
/// This is the one primitive operation that all other operations are built
/// upon.
///
/// You might prefer to use [`ServiceObjectsExt::insert`], which is a
/// convenient wrapper around `Service<Objects>::execute`.
InsertObject {
/// The object to insert
object: Object<WithHandle>,
},
}

/// Event produced by `Service<Objects>`
#[derive(Clone, Debug)]
pub struct ObjectToInsert {
pub struct InsertObject {
/// The object to insert
pub object: Object<WithHandle>,
}

/// Convenient API for `Service<Objects>`
pub trait ServiceObjectsExt {
/// Insert an object
fn insert<T>(&mut self, handle: Handle<T>, object: T)
where
(Handle<T>, T): Into<Object<WithHandle>>;
fn insert(&mut self, object: Object<WithHandle>);
}

impl ServiceObjectsExt for Service<Objects> {
fn insert<T>(&mut self, handle: Handle<T>, object: T)
where
(Handle<T>, T): Into<Object<WithHandle>>,
{
self.execute(InsertObject {
object: (handle, object).into(),
});
fn insert(&mut self, object: Object<WithHandle>) {
self.execute(Operation::InsertObject { object });
}
}
4 changes: 2 additions & 2 deletions crates/fj-kernel/src/services/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
validate::ValidationError,
};

use super::{objects::ObjectToInsert, State};
use super::{objects::InsertObject, State};

/// Errors that occurred while validating the objects inserted into the stores
#[derive(Default)]
Expand All @@ -33,7 +33,7 @@ impl Drop for Validation {
}

impl State for Validation {
type Command = ObjectToInsert;
type Command = InsertObject;
type Event = ValidationFailed;

fn decide(&self, command: Self::Command, events: &mut Vec<Self::Event>) {
Expand Down