diff --git a/examples/structtest.rs b/examples/structtest.rs new file mode 100644 index 0000000000000000000000000000000000000000..65cb32c43797e218cc3112d0454c10ade4054a09 --- /dev/null +++ b/examples/structtest.rs @@ -0,0 +1,59 @@ +use petgraph::prelude::{DiGraph, EdgeIndex, NodeIndex}; +struct OpticDummy; + +struct OpticIdealLens { + focal_length: f64, +} +impl OpticIdealLens { + pub fn new(focal_length: f64) -> Self { + Self{focal_length} + } +} +trait Optical { + fn analyze(&self) { + println!("generic analyze"); + } +} +impl Optical for OpticDummy { + fn analyze(&self) { + println!("optic dummy analyze"); + } +} +impl Optical for OpticIdealLens { + fn analyze(&self) { + println!("ideal lens analyze. f={}",self.focal_length); + } +} +struct OpticNode<T: Optical> { + name: String, + node: T +} +impl <T: Optical> OpticNode<T> { + pub fn new(name: &str, t: T) -> Self { + Self{name: name.into(), node : t} + } + // pub fn node_mut(&mut self) -> &mut T { + // &mut self.node + // } + pub fn analyze(&mut self) { + print!("Analyze element {}: ",self.name); + self.node.analyze(); + } +} + +impl OpticNode<OpticIdealLens> { + pub fn set_focal_length(&mut self, f: f64) { + self.node.focal_length=f; + } +} +fn main() { + let mut node=OpticNode::new("Test1", OpticDummy); + node.analyze(); + + let mut node=OpticNode::new("Test2", OpticIdealLens::new(1.23)); + node.analyze(); + node.set_focal_length(3.45); + node.analyze(); + + // let g: DiGraph<OpticNode<>,()>=DiGraph::new(); // does not work since it needs a concrete type here.... +} \ No newline at end of file diff --git a/examples/structtest2.rs b/examples/structtest2.rs new file mode 100644 index 0000000000000000000000000000000000000000..7c6914ef2c0d6fa10502bbc9bab589b34c500f6b --- /dev/null +++ b/examples/structtest2.rs @@ -0,0 +1,61 @@ +use petgraph::prelude::{DiGraph, EdgeIndex, NodeIndex}; +trait Optical { + fn analyze(&self) { + println!("generic analyze"); + } +} +struct OpticDummy; + +struct OpticIdealLens { + focal_length: f64, +} + +impl OpticIdealLens { + pub fn new(focal_length: f64) -> Self { + Self{focal_length} + } +} + +impl Optical for OpticIdealLens { + fn analyze(&self) { + println!("ideal lens analyze: f={}",self.focal_length); + } +} +enum NodeType { + Dummy, + IdealLens(OpticIdealLens), + SimpleElement(f64) +} +impl NodeType { + fn analyze(&self) { + match self { + NodeType::Dummy => println!("dummy -> nothing to do here"), + NodeType::IdealLens(n) => n.analyze(), + _ => println!("not covered") + } + } +} +struct OpticNode { + name: String, + node: NodeType +} + +impl OpticNode { + fn new(name: &str, node_type: NodeType) -> Self { + Self{name: name.into(), node: node_type} + } + fn analyze(&self) { + print!("Analyze {}: ",self.name); + self.node.analyze(); + } +} +fn main() { + let node=OpticNode::new("Test1",NodeType::Dummy); + node.analyze(); + let node=OpticNode::new("Test2",NodeType::IdealLens(OpticIdealLens::new(1.23))); + node.analyze(); + let node=OpticNode::new("Test2",NodeType::SimpleElement(1.23)); + node.analyze(); + + let p:DiGraph<OpticNode,()> = DiGraph::new(); +} \ No newline at end of file