From 7e8ee5ed9cad6484e9f13f81731b102ced58402e Mon Sep 17 00:00:00 2001 From: Adam Carpenter Date: Tue, 9 Jul 2019 15:14:04 -0400 Subject: Init. --- meap/meap-code/ch4/ch4-check-sats-1.rs | 27 +++++++ meap/meap-code/ch4/ch4-check-sats-3.rs | 33 +++++++++ .../ch4/ch4-check-sats-clone-and-copy-traits.rs | 24 +++++++ .../ch4/ch4-check-sats-with-copy-trait.rs | 46 ++++++++++++ meap/meap-code/ch4/ch4-rc-groundstation.rs | 10 +++ meap/meap-code/ch4/ch4-rc-refcell-groundstation.rs | 31 ++++++++ meap/meap-code/ch4/ch4-sat-mailbox.rs | 42 +++++++++++ meap/meap-code/ch4/ch4-short-lived-strategy.rs | 82 ++++++++++++++++++++++ 8 files changed, 295 insertions(+) create mode 100755 meap/meap-code/ch4/ch4-check-sats-1.rs create mode 100755 meap/meap-code/ch4/ch4-check-sats-3.rs create mode 100755 meap/meap-code/ch4/ch4-check-sats-clone-and-copy-traits.rs create mode 100755 meap/meap-code/ch4/ch4-check-sats-with-copy-trait.rs create mode 100755 meap/meap-code/ch4/ch4-rc-groundstation.rs create mode 100755 meap/meap-code/ch4/ch4-rc-refcell-groundstation.rs create mode 100755 meap/meap-code/ch4/ch4-sat-mailbox.rs create mode 100755 meap/meap-code/ch4/ch4-short-lived-strategy.rs (limited to 'meap/meap-code/ch4') diff --git a/meap/meap-code/ch4/ch4-check-sats-1.rs b/meap/meap-code/ch4/ch4-check-sats-1.rs new file mode 100755 index 0000000..254c866 --- /dev/null +++ b/meap/meap-code/ch4/ch4-check-sats-1.rs @@ -0,0 +1,27 @@ +#![allow(unused_variables)] + +#[derive(Debug)] +enum StatusMessage { + Ok, +} + +fn check_status(sat_id: u64) -> StatusMessage { + StatusMessage::Ok +} + +fn main () { + let sat_a = 0; + let sat_b = 1; + let sat_c = 2; + + let a_status = check_status(sat_a); + let b_status = check_status(sat_b); + let c_status = check_status(sat_c); + println!("a: {:?}, b: {:?}, c: {:?}", a_status, b_status, c_status); + + // "waiting" ... + let a_status = check_status(sat_a); + let b_status = check_status(sat_b); + let c_status = check_status(sat_c); + println!("a: {:?}, b: {:?}, c: {:?}", a_status, b_status, c_status); +} diff --git a/meap/meap-code/ch4/ch4-check-sats-3.rs b/meap/meap-code/ch4/ch4-check-sats-3.rs new file mode 100755 index 0000000..58a65cd --- /dev/null +++ b/meap/meap-code/ch4/ch4-check-sats-3.rs @@ -0,0 +1,33 @@ +#![allow(unused_variables)] + +#[derive(Debug)] +struct CubeSat { + id: u64, +} + +#[derive(Debug)] +enum StatusMessage { + Ok, +} + +fn check_status(sat_id: CubeSat) -> CubeSat { + println!("{:?}: {:?}", sat_id, StatusMessage::Ok); + sat_id +} + +fn main () { + let sat_a = CubeSat { id: 0 }; + let sat_b = CubeSat { id: 1 }; + let sat_c = CubeSat { id: 2 }; + + let sat_a = check_status(sat_a); + let sat_b = check_status(sat_b); + let sat_c = check_status(sat_c); + // println!("a: {:?}, b: {:?}, c: {:?}", a_status, b_status, c_status); + + // "waiting" ... + let sat_a = check_status(sat_a); + let sat_b = check_status(sat_b); + let sat_c = check_status(sat_c); + //println!("a: {:?}, b: {:?}, c: {:?}", a_status, b_status, c_status); +} diff --git a/meap/meap-code/ch4/ch4-check-sats-clone-and-copy-traits.rs b/meap/meap-code/ch4/ch4-check-sats-clone-and-copy-traits.rs new file mode 100755 index 0000000..df17ed5 --- /dev/null +++ b/meap/meap-code/ch4/ch4-check-sats-clone-and-copy-traits.rs @@ -0,0 +1,24 @@ +#[derive(Debug,Copy)] +struct CubeSat { + id: u64, +} + +#[derive(Debug,Copy)] +enum StatusMessage { + Ok, +} + + +fn check_status(sat_id: CubeSat) -> StatusMessage { + StatusMessage::Ok +} + +fn main () { + let sat_a = CubeSat { id: 0 }; + + let a_status = check_status(sat_a.clone()); + println!("a: {:?}", a_status.clone(); + + let a_status = check_status(sat_a); + println!("a: {:?}", a_status); +} diff --git a/meap/meap-code/ch4/ch4-check-sats-with-copy-trait.rs b/meap/meap-code/ch4/ch4-check-sats-with-copy-trait.rs new file mode 100755 index 0000000..4126e05 --- /dev/null +++ b/meap/meap-code/ch4/ch4-check-sats-with-copy-trait.rs @@ -0,0 +1,46 @@ +#[derive(Debug)] +struct CubeSat { + id: u64, +} + +#[derive(Debug)] +enum StatusMessage { + Ok, +} + +impl Copy for CubeSat { } + +impl Copy for StatusMessage { } + +impl Clone for CubeSat { // <1> + fn clone(&self) -> Self { + CubeSat { id: self.id } //<2> + } +} + +impl Clone for StatusMessage { + fn clone(&self) -> Self { + *self // <3> + } +} + +fn check_status(sat_id: CubeSat) -> StatusMessage { + StatusMessage::Ok +} + +fn main () { + let sat_a = CubeSat { id: 0 }; + let sat_b = CubeSat { id: 1 }; + let sat_c = CubeSat { id: 2 }; + + let a_status = check_status(sat_a); + let b_status = check_status(sat_b); + let c_status = check_status(sat_c); + println!("a: {:?}, b: {:?}, c: {:?}", a_status, b_status, c_status); + + // "waiting" ... + let a_status = check_status(sat_a); + let b_status = check_status(sat_b); + let c_status = check_status(sat_c); + println!("a: {:?}, b: {:?}, c: {:?}", a_status, b_status, c_status); +} diff --git a/meap/meap-code/ch4/ch4-rc-groundstation.rs b/meap/meap-code/ch4/ch4-rc-groundstation.rs new file mode 100755 index 0000000..c51f865 --- /dev/null +++ b/meap/meap-code/ch4/ch4-rc-groundstation.rs @@ -0,0 +1,10 @@ +use std::rc::Rc; + +#[derive(Debug)] +struct GroundStation {} + +fn main() { + let base: Rc = Rc::new(GroundStation {}); + + println!("{:?}", base); +} \ No newline at end of file diff --git a/meap/meap-code/ch4/ch4-rc-refcell-groundstation.rs b/meap/meap-code/ch4/ch4-rc-refcell-groundstation.rs new file mode 100755 index 0000000..d0e27ee --- /dev/null +++ b/meap/meap-code/ch4/ch4-rc-refcell-groundstation.rs @@ -0,0 +1,31 @@ +use std::rc::Rc; +use std::cell::RefCell; + +#[derive(Debug)] +struct GroundStation { + radio_freq: f64 // Mhz +} + +fn main() { + let base: Rc> = Rc::new(RefCell::new( + GroundStation { + radio_freq: 87.65 + } + )); + + println!("base: {:?}", base); + + { // introduce a new scope + let mut base_2 = base.borrow_mut(); + base_2.radio_freq -= 12.34; + println!("base_2: {:?}", base_2); + } + + println!("base: {:?}", base); + + let mut base_3 = base.borrow_mut(); + base_3.radio_freq += 43.21; + + println!("base: {:?}", base); + println!("base_3: {:?}", base_3); +} diff --git a/meap/meap-code/ch4/ch4-sat-mailbox.rs b/meap/meap-code/ch4/ch4-sat-mailbox.rs new file mode 100755 index 0000000..81f292d --- /dev/null +++ b/meap/meap-code/ch4/ch4-sat-mailbox.rs @@ -0,0 +1,42 @@ + +#[derive(Debug)] +struct CubeSat { + id: u64, + mailbox: Mailbox, +} + +#[derive(Debug)] +struct Mailbox { + messages: Vec, +} + +type Message = String; + +struct GroundStation; + +impl GroundStation { + fn send(&self, to: &mut CubeSat, msg: Message) { + to.mailbox.messages.push(msg); + } +} + +impl CubeSat { + fn recv(&mut self) -> Option { + self.mailbox.messages.pop() + } +} + +fn main() { + let base = GroundStation {}; + let mut sat_a = CubeSat { id: 0, mailbox: Mailbox { messages: vec![] } }; + + println!("t0: {:?}", sat_a); + base.send(&mut sat_a, Message::from("hello there!")); // <1> + + println!("t1: {:?}", sat_a); + + let msg = sat_a.recv(); + println!("t2: {:?}", sat_a); + + println!("msg: {:?}", msg); +} diff --git a/meap/meap-code/ch4/ch4-short-lived-strategy.rs b/meap/meap-code/ch4/ch4-short-lived-strategy.rs new file mode 100755 index 0000000..1082469 --- /dev/null +++ b/meap/meap-code/ch4/ch4-short-lived-strategy.rs @@ -0,0 +1,82 @@ +#![allow(unused_variables)] + +#[derive(Debug)] +struct CubeSat { + id: u64, +} + +#[derive(Debug)] +struct Mailbox { + messages: Vec, +} + +#[derive(Debug)] +struct Message { + to: u64, + content: String, +} + +struct GroundStation {} + +impl Mailbox { + fn post(&mut self, msg: Message) { + self.messages.push(msg); + } + + fn deliver(&mut self, recipient: &CubeSat) -> Option { + for i in 0..self.messages.len() { + if self.messages[i].to == recipient.id { + let msg = self.messages.remove(i); + return Some(msg); + } + } + + None + } +} + +impl GroundStation { + fn connect(&self, sat_id: u64) -> CubeSat { + CubeSat { + id: sat_id, + } + } + + fn send(&self, mailbox: &mut Mailbox, msg: Message) { + mailbox.post(msg); + } +} + +impl CubeSat { + fn recv(&self, mailbox: &mut Mailbox) -> Option { + mailbox.deliver(&self) + } +} + +fn fetch_sat_ids() -> Vec { + vec![1,2,3] +} + + +fn main() { + let mut mail = Mailbox { messages: vec![] }; + + let base = GroundStation {}; + + let sat_ids = fetch_sat_ids(); + + for sat_id in sat_ids { + let sat = base.connect(sat_id); + let msg = Message { to: sat_id, content: String::from("hello") }; + base.send(&mut mail, msg); + } + + let sat_ids = fetch_sat_ids(); + + for sat_id in sat_ids { + let sat = base.connect(sat_id); + + let msg = sat.recv(&mut mail); + println!("{:?}: {:?}", sat, msg); + } +} \ No newline at end of file -- cgit v1.2.3