Add AnyOp

This commit is contained in:
Hanno Braun 2024-12-11 20:20:33 +01:00
parent c28e5e0771
commit 2cfa6ed6b0
2 changed files with 36 additions and 5 deletions

View File

@ -1,4 +1,4 @@
use std::fmt;
use std::{fmt, rc::Rc};
use super::{Triangle, Vertex};
@ -7,3 +7,34 @@ pub trait Operation: fmt::Display {
fn triangles(&self, triangles: &mut Vec<Triangle>);
fn children(&self) -> Vec<Box<dyn Operation>>;
}
#[derive(Clone)]
pub struct AnyOp {
inner: Rc<dyn Operation>,
}
impl AnyOp {
pub fn new(op: impl Operation + 'static) -> Self {
Self { inner: Rc::new(op) }
}
}
impl fmt::Display for AnyOp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.inner)
}
}
impl Operation for AnyOp {
fn vertices(&self, vertices: &mut Vec<Vertex>) {
self.inner.vertices(vertices);
}
fn triangles(&self, triangles: &mut Vec<Triangle>) {
self.inner.triangles(triangles);
}
fn children(&self) -> Vec<Box<dyn Operation>> {
self.inner.children()
}
}

View File

@ -2,7 +2,7 @@ use std::{fmt, rc::Rc};
use tuples::CombinRight;
use super::{Operation, Triangle, Vertex};
use super::{operation::AnyOp, Operation, Triangle, Vertex};
#[derive(Default)]
pub struct OpsLog {
@ -17,7 +17,7 @@ impl OpsLog {
let vertex = vertex.into();
self.operations.push(OperationInSequence {
operation: Rc::new(vertex),
operation: AnyOp::new(vertex),
previous: self.operations.last().map(|op| Rc::new(op.clone()) as _),
});
@ -34,7 +34,7 @@ impl OpsLog {
let triangle = triangle.into();
self.operations.push(OperationInSequence {
operation: Rc::new(triangle),
operation: AnyOp::new(triangle),
previous: self.operations.last().map(|op| Rc::new(op.clone()) as _),
});
@ -78,7 +78,7 @@ impl Operation for OpsLog {
#[derive(Clone)]
struct OperationInSequence {
pub operation: Rc<dyn Operation>,
pub operation: AnyOp,
pub previous: Option<Rc<dyn Operation>>,
}