Skip to content
Snippets Groups Projects
Commit 0750abdc authored by Udo Eisenbarth's avatar Udo Eisenbarth :speech_balloon:
Browse files

Add two examples for testing the overall node structure.

parent fa113e87
No related branches found
No related tags found
No related merge requests found
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
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment