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>
impl<T: Clone, const N: usize> Port<T, N>
Sourcepub fn add_value(&mut self, item: T) -> Result<(), T>
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 }
Sourcepub fn get_values(&self) -> &[T]
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more