Struct Port

Source
pub struct Port<T: Clone, const N: usize>(/* private fields */);
Expand description

Port is a generic structure that can be used to store values of any type T. It is the main artifact to exchange data between components. Note that, in no_std environments, the capacity of the port N must be known at compile time.

Implementations§

Source§

impl<T: Clone, const N: usize> Port<T, N>

Source

pub const fn new() -> Self

Creates a new empty port.

Source

pub fn is_empty(&self) -> bool

Returns true if the port is empty.

Source

pub fn is_full(&self) -> bool

Returns true if the port is full.

Source

pub fn len(&self) -> usize

Returns the number of elements in the port.

Source

pub fn clear(&mut self)

Clears the port, removing all values.

Source

pub fn add_value(&mut self, item: T) -> Result<(), T>

Adds a value to the port.

Examples found in repository?
examples/gpt.rs (line 37)
35        fn lambda(state: &Self::State, output: &mut Self::Output) {
36            println!("[G] sending job {}", state.count);
37            output.out_job.add_value(state.count).unwrap();
38        }
39
40        fn ta(state: &Self::State) -> f64 {
41            state.sigma
42        }
43
44        fn delta_ext(state: &mut Self::State, e: f64, x: &Self::Input) {
45            state.sigma -= e;
46            if let Some(&stop) = x.in_stop.get_values().last() {
47                println!("[G] received stop: {}", stop);
48                if stop {
49                    state.sigma = f64::INFINITY;
50                }
51            }
52        }
53    }
54}
55
56mod processor {
57    pub struct ProcessorState {
58        sigma: f64,
59        time: f64,
60        job: Option<usize>,
61    }
62
63    impl ProcessorState {
64        pub fn new(time: f64) -> Self {
65            Self {
66                sigma: 0.0,
67                time,
68                job: None,
69            }
70        }
71    }
72
73    xdevs::component!(
74        ident = Processor,
75        input = {
76            in_job<usize, 1>
77        },
78        output = {
79            out_job<usize>
80        },
81        state = ProcessorState,
82    );
83
84    impl xdevs::Atomic for Processor {
85        fn delta_int(state: &mut Self::State) {
86            state.sigma = f64::INFINITY;
87            if let Some(job) = state.job {
88                println!("[P] processed job {}", job);
89                state.job = None;
90            }
91        }
92
93        fn lambda(state: &Self::State, output: &mut Self::Output) {
94            if let Some(job) = state.job {
95                output.out_job.add_value(job).unwrap();
96            }
97        }
98
99        fn ta(state: &Self::State) -> f64 {
100            state.sigma
101        }
102
103        fn delta_ext(state: &mut Self::State, e: f64, x: &Self::Input) {
104            state.sigma -= e;
105            if let Some(&job) = x.in_job.get_values().last() {
106                print!("[P] received job {}", job);
107                if state.job.is_none() {
108                    println!(" (idle)");
109                    state.job = Some(job);
110                    state.sigma = state.time;
111                } else {
112                    println!(" (busy)");
113                }
114            }
115        }
116    }
117}
118
119mod transducer {
120    pub struct TransducerState {
121        sigma: f64,
122        clock: f64,
123        n_generated: usize,
124        n_processed: usize,
125    }
126
127    impl TransducerState {
128        pub fn new(obs_time: f64) -> Self {
129            Self {
130                sigma: obs_time,
131                clock: 0.0,
132                n_generated: 0,
133                n_processed: 0,
134            }
135        }
136    }
137
138    xdevs::component!(
139        ident = Transducer,
140        input = {
141            in_generator<usize, 1>,
142            in_processor<usize, 1>,
143        },
144        output = {
145            out_stop<bool>
146        },
147        state = TransducerState,
148    );
149
150    impl xdevs::Atomic for Transducer {
151        fn delta_int(state: &mut Self::State) {
152            state.clock += state.sigma;
153            let (acceptance, throughput) = if state.n_processed > 0 {
154                (
155                    state.n_processed as f64 / state.n_generated as f64,
156                    state.n_processed as f64 / state.clock,
157                )
158            } else {
159                (0.0, 0.0)
160            };
161            println!(
162                "[T] acceptance: {:.2}, throughput: {:.2}",
163                acceptance, throughput
164            );
165            state.sigma = f64::INFINITY;
166        }
167
168        fn lambda(_state: &Self::State, output: &mut Self::Output) {
169            output.out_stop.add_value(true).unwrap();
170        }
Source

pub fn add_values(&mut self, items: &[T]) -> Result<(), ()>

Adds multiple values to the port.

Source

pub fn get_values(&self) -> &[T]

Returns a slice of the port’s values.

Examples found in repository?
examples/gpt.rs (line 46)
44        fn delta_ext(state: &mut Self::State, e: f64, x: &Self::Input) {
45            state.sigma -= e;
46            if let Some(&stop) = x.in_stop.get_values().last() {
47                println!("[G] received stop: {}", stop);
48                if stop {
49                    state.sigma = f64::INFINITY;
50                }
51            }
52        }
53    }
54}
55
56mod processor {
57    pub struct ProcessorState {
58        sigma: f64,
59        time: f64,
60        job: Option<usize>,
61    }
62
63    impl ProcessorState {
64        pub fn new(time: f64) -> Self {
65            Self {
66                sigma: 0.0,
67                time,
68                job: None,
69            }
70        }
71    }
72
73    xdevs::component!(
74        ident = Processor,
75        input = {
76            in_job<usize, 1>
77        },
78        output = {
79            out_job<usize>
80        },
81        state = ProcessorState,
82    );
83
84    impl xdevs::Atomic for Processor {
85        fn delta_int(state: &mut Self::State) {
86            state.sigma = f64::INFINITY;
87            if let Some(job) = state.job {
88                println!("[P] processed job {}", job);
89                state.job = None;
90            }
91        }
92
93        fn lambda(state: &Self::State, output: &mut Self::Output) {
94            if let Some(job) = state.job {
95                output.out_job.add_value(job).unwrap();
96            }
97        }
98
99        fn ta(state: &Self::State) -> f64 {
100            state.sigma
101        }
102
103        fn delta_ext(state: &mut Self::State, e: f64, x: &Self::Input) {
104            state.sigma -= e;
105            if let Some(&job) = x.in_job.get_values().last() {
106                print!("[P] received job {}", job);
107                if state.job.is_none() {
108                    println!(" (idle)");
109                    state.job = Some(job);
110                    state.sigma = state.time;
111                } else {
112                    println!(" (busy)");
113                }
114            }
115        }
116    }
117}
118
119mod transducer {
120    pub struct TransducerState {
121        sigma: f64,
122        clock: f64,
123        n_generated: usize,
124        n_processed: usize,
125    }
126
127    impl TransducerState {
128        pub fn new(obs_time: f64) -> Self {
129            Self {
130                sigma: obs_time,
131                clock: 0.0,
132                n_generated: 0,
133                n_processed: 0,
134            }
135        }
136    }
137
138    xdevs::component!(
139        ident = Transducer,
140        input = {
141            in_generator<usize, 1>,
142            in_processor<usize, 1>,
143        },
144        output = {
145            out_stop<bool>
146        },
147        state = TransducerState,
148    );
149
150    impl xdevs::Atomic for Transducer {
151        fn delta_int(state: &mut Self::State) {
152            state.clock += state.sigma;
153            let (acceptance, throughput) = if state.n_processed > 0 {
154                (
155                    state.n_processed as f64 / state.n_generated as f64,
156                    state.n_processed as f64 / state.clock,
157                )
158            } else {
159                (0.0, 0.0)
160            };
161            println!(
162                "[T] acceptance: {:.2}, throughput: {:.2}",
163                acceptance, throughput
164            );
165            state.sigma = f64::INFINITY;
166        }
167
168        fn lambda(_state: &Self::State, output: &mut Self::Output) {
169            output.out_stop.add_value(true).unwrap();
170        }
171
172        fn ta(state: &Self::State) -> f64 {
173            state.sigma
174        }
175
176        fn delta_ext(state: &mut Self::State, e: f64, x: &Self::Input) {
177            state.sigma -= e;
178            state.clock += e;
179            state.n_generated += x.in_generator.get_values().len();
180            state.n_processed += x.in_processor.get_values().len();
181        }

Trait Implementations§

Source§

impl<T: Clone, const N: usize> Bag for Port<T, N>

Source§

fn is_empty(&self) -> bool

Returns true if the ports are empty.
Source§

fn clear(&mut self)

Clears the ports, removing all values.
Source§

impl<T: Debug + Clone, const N: usize> Debug for Port<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default + Clone, const N: usize> Default for Port<T, N>

Source§

fn default() -> Port<T, N>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for Port<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for Port<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for Port<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for Port<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for Port<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for Port<T, N>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.