summaryrefslogtreecommitdiffstats
path: root/src/doc/book/listings/ch17-oop/listing-17-09
diff options
context:
space:
mode:
Diffstat (limited to 'src/doc/book/listings/ch17-oop/listing-17-09')
-rw-r--r--src/doc/book/listings/ch17-oop/listing-17-09/Cargo.lock6
-rw-r--r--src/doc/book/listings/ch17-oop/listing-17-09/Cargo.toml6
-rw-r--r--src/doc/book/listings/ch17-oop/listing-17-09/src/lib.rs27
-rw-r--r--src/doc/book/listings/ch17-oop/listing-17-09/src/main.rs40
4 files changed, 79 insertions, 0 deletions
diff --git a/src/doc/book/listings/ch17-oop/listing-17-09/Cargo.lock b/src/doc/book/listings/ch17-oop/listing-17-09/Cargo.lock
new file mode 100644
index 000000000..00d7b2182
--- /dev/null
+++ b/src/doc/book/listings/ch17-oop/listing-17-09/Cargo.lock
@@ -0,0 +1,6 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "gui"
+version = "0.1.0"
+
diff --git a/src/doc/book/listings/ch17-oop/listing-17-09/Cargo.toml b/src/doc/book/listings/ch17-oop/listing-17-09/Cargo.toml
new file mode 100644
index 000000000..9b816e766
--- /dev/null
+++ b/src/doc/book/listings/ch17-oop/listing-17-09/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "gui"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/src/doc/book/listings/ch17-oop/listing-17-09/src/lib.rs b/src/doc/book/listings/ch17-oop/listing-17-09/src/lib.rs
new file mode 100644
index 000000000..960fee23d
--- /dev/null
+++ b/src/doc/book/listings/ch17-oop/listing-17-09/src/lib.rs
@@ -0,0 +1,27 @@
+pub trait Draw {
+ fn draw(&self);
+}
+
+pub struct Screen {
+ pub components: Vec<Box<dyn Draw>>,
+}
+
+impl Screen {
+ pub fn run(&self) {
+ for component in self.components.iter() {
+ component.draw();
+ }
+ }
+}
+
+pub struct Button {
+ pub width: u32,
+ pub height: u32,
+ pub label: String,
+}
+
+impl Draw for Button {
+ fn draw(&self) {
+ // code to actually draw a button
+ }
+}
diff --git a/src/doc/book/listings/ch17-oop/listing-17-09/src/main.rs b/src/doc/book/listings/ch17-oop/listing-17-09/src/main.rs
new file mode 100644
index 000000000..4eb13f6b7
--- /dev/null
+++ b/src/doc/book/listings/ch17-oop/listing-17-09/src/main.rs
@@ -0,0 +1,40 @@
+use gui::Draw;
+
+struct SelectBox {
+ width: u32,
+ height: u32,
+ options: Vec<String>,
+}
+
+impl Draw for SelectBox {
+ fn draw(&self) {
+ // code to actually draw a select box
+ }
+}
+
+// ANCHOR: here
+use gui::{Button, Screen};
+
+fn main() {
+ let screen = Screen {
+ components: vec![
+ Box::new(SelectBox {
+ width: 75,
+ height: 10,
+ options: vec![
+ String::from("Yes"),
+ String::from("Maybe"),
+ String::from("No"),
+ ],
+ }),
+ Box::new(Button {
+ width: 50,
+ height: 10,
+ label: String::from("OK"),
+ }),
+ ],
+ };
+
+ screen.run();
+}
+// ANCHOR_END: here