Refer to Triangle via new Handle

This commit is contained in:
Hanno Braun 2024-12-13 20:03:41 +01:00
parent b368105d34
commit 1122367341
2 changed files with 43 additions and 5 deletions

View File

@ -8,6 +8,41 @@ pub trait Operation: fmt::Display {
fn children(&self) -> Vec<HandleAny>; fn children(&self) -> Vec<HandleAny>;
} }
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Handle<T> {
inner: Rc<T>,
}
impl<T> Handle<T> {
pub fn new(inner: T) -> Self {
Self {
inner: Rc::new(inner),
}
}
pub fn to_any(&self) -> HandleAny
where
T: Operation + 'static,
{
self.clone().into_any()
}
pub fn into_any(self) -> HandleAny
where
T: Operation + 'static,
{
HandleAny { inner: self.inner }
}
}
impl<T> Clone for Handle<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct HandleAny { pub struct HandleAny {
inner: Rc<dyn Operation>, inner: Rc<dyn Operation>,

View File

@ -2,7 +2,10 @@ use std::fmt;
use tuples::CombinRight; use tuples::CombinRight;
use super::{operation::HandleAny, Operation, Triangle, Vertex}; use super::{
operation::{Handle, HandleAny},
Operation, Triangle, Vertex,
};
#[derive(Default)] #[derive(Default)]
pub struct Shape { pub struct Shape {
@ -33,11 +36,11 @@ impl Shape {
pub fn triangle( pub fn triangle(
&mut self, &mut self,
triangle: impl Into<Triangle>, triangle: impl Into<Triangle>,
) -> OperationResult<(Triangle,)> { ) -> OperationResult<(Handle<Triangle>,)> {
let triangle = triangle.into(); let triangle = Handle::new(triangle.into());
self.operations.push(OperationInSequence { self.operations.push(OperationInSequence {
operation: HandleAny::new(triangle.clone()), operation: triangle.to_any(),
previous: self previous: self
.operations .operations
.last() .last()
@ -138,7 +141,7 @@ impl<'r, T> OperationResult<'r, T> {
triangle: impl Into<Triangle>, triangle: impl Into<Triangle>,
) -> OperationResult<'r, T::Out> ) -> OperationResult<'r, T::Out>
where where
T: CombinRight<Triangle>, T: CombinRight<Handle<Triangle>>,
{ {
let OperationResult { let OperationResult {
results: (triangle,), results: (triangle,),