Skip to content

Commit

Permalink
FIX: Make sure lib.rs is implemented correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
bsodmike committed Nov 17, 2022
1 parent bb093b4 commit 5018e48
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 48 deletions.
12 changes: 6 additions & 6 deletions src/lib/builder.rs → src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
#[derive(Debug)]
pub(crate) struct TaskManager {
pub struct TaskManager {
state: String,
count: usize,
}

impl TaskManager {
/// Get task count
pub(crate) fn count(&self) -> &usize {
pub fn count(&self) -> &usize {
&self.count
}
}

#[derive(Default)]
pub(crate) struct TaskManagerBuilder {
pub struct TaskManagerBuilder {
state: String,
count: usize,
}

impl TaskManagerBuilder {
/// Creates a new TaskManagerBuilder
pub(crate) fn new() -> Self {
pub fn new() -> Self {
Self {
state: "initialized".to_string(),
count: 0,
}
}

/// Sets the task count
pub(crate) fn count(mut self, value: usize) -> Self {
pub fn count(mut self, value: usize) -> Self {
self.count = value;
self
}

/// Creates a new TaskManager
pub(crate) fn build(self) -> TaskManager {
pub fn build(self) -> TaskManager {
TaskManager {
state: self.state,
count: self.count,
Expand Down
8 changes: 4 additions & 4 deletions src/lib/cli.rs → src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use std::str::FromStr;
/// Program to run rust tutorials
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
pub(crate) struct Args {
pub struct Args {
#[clap(subcommand)]
pub(crate) command: Option<Commands>,
pub command: Option<Commands>,
}

#[derive(Subcommand, Debug)]
pub(crate) enum Commands {
pub enum Commands {
/// Tutorial on dynamic dispatch
Dispatch,

Expand All @@ -31,6 +31,6 @@ pub(crate) enum Commands {
Conversion,
}

pub(crate) fn runner<T>(mut mk: impl FnMut() -> Result<T>) -> Result<T> {
pub fn runner<T>(mut mk: impl FnMut() -> Result<T>) -> Result<T> {
mk()
}
3 changes: 3 additions & 0 deletions src/lib/conversion/mod.rs → src/conversion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub fn lesson_1() -> Result<()> {
let resp2 = u16::from(value);
assert_eq!(resp, resp2);

let formal = <u8 as Into<u16>>::into(value);
let shorter = value as u16;

Ok(())
}

Expand Down
12 changes: 6 additions & 6 deletions src/lib/dispatch.rs → src/dispatch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Say hello in Norwegian
pub(crate) trait Hei {
pub trait Hei {
fn hei(&self);

fn weird(&self);
Expand Down Expand Up @@ -37,23 +37,23 @@ impl Hei for String {
}
}

pub(crate) fn say_hei(s: &dyn Hei) {
pub fn say_hei(s: &dyn Hei) {
s.hei()
}

pub(crate) fn strlen<S: AsRef<str>>(s: S) -> usize {
pub fn strlen<S: AsRef<str>>(s: S) -> usize {
s.as_ref().len()
}

pub(crate) fn strlen2(s: String) -> usize {
pub fn strlen2(s: String) -> usize {
s.len()
}

// examples of trait objects
pub(crate) fn strlen_dyn2(s: Box<dyn AsRef<str>>) -> usize {
pub fn strlen_dyn2(s: Box<dyn AsRef<str>>) -> usize {
s.as_ref().as_ref().len()
}

pub(crate) fn strlen_dyn(s: &dyn AsRef<str>) -> usize {
pub fn strlen_dyn(s: &dyn AsRef<str>) -> usize {
s.as_ref().len()
}
File renamed without changes.
11 changes: 5 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@

use anyhow::{Error, Result};
use clap::Parser;
use lib::cli::{runner, Args, Commands};
use lib::{builder::TaskManagerBuilder, dispatch::*, oop_pattern::*, smart_pointers::*, traits};
use log::{debug, info};
use std::io::Read;
use std::sync::Arc;

pub mod lib;
// pub mod sandbox;
use tutorials::cli::{runner, Args, Commands};
use tutorials::{
builder::TaskManagerBuilder, dispatch::*, oop_pattern::*, smart_pointers::*, traits,
};

fn main() -> Result<()> {
env_logger::init();
Expand Down Expand Up @@ -131,7 +130,7 @@ fn main() -> Result<()> {
Some(Commands::Conversion) => runner(|| {
info!("Tutorial: Conversion\n");

lib::conversion::runner()?;
tutorials::conversion::runner()?;

Ok(())
})?,
Expand Down
File renamed without changes.
26 changes: 13 additions & 13 deletions src/lib/oop_pattern.rs → src/oop_pattern.rs
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
pub(crate) struct Post {
pub struct Post {
content: String,
}

pub(crate) struct DraftPost {
pub struct DraftPost {
content: String,
}

impl Post {
pub(crate) fn new() -> DraftPost {
pub fn new() -> DraftPost {
DraftPost {
content: String::new(),
}
}

pub(crate) fn content(&self) -> &str {
pub fn content(&self) -> &str {
&self.content
}
}

impl DraftPost {
pub(crate) fn add_text(&mut self, text: &str) {
pub fn add_text(&mut self, text: &str) {
self.content.push_str(text);
}

pub(crate) fn request_review(self) -> PendingReviewPost {
pub fn request_review(self) -> PendingReviewPost {
PendingReviewPost {
content: self.content,
}
}
}

pub(crate) struct PendingReviewPost {
pub struct PendingReviewPost {
content: String,
}

impl PendingReviewPost {
pub(crate) fn review(&self) -> &String {
pub fn review(&self) -> &String {
&self.content
}

pub(crate) fn approve(self) -> Post {
pub fn approve(self) -> Post {
Post {
content: self.content,
}
}

pub(crate) fn reject(self, changes: &str) -> RequestChangesPost {
pub fn reject(self, changes: &str) -> RequestChangesPost {
RequestChangesPost {
content: self.content,
changes: changes.to_string(),
}
}
}

pub(crate) struct RequestChangesPost {
pub struct RequestChangesPost {
content: String,
changes: String,
}

impl RequestChangesPost {
pub(crate) fn get_feedback(&self) -> String {
pub fn get_feedback(&self) -> String {
format!("Make changes to '{}' as {}", &self.content, &self.changes)
}

pub(crate) fn replace_text(&mut self, text: &str) -> PendingReviewPost {
pub fn replace_text(&mut self, text: &str) -> PendingReviewPost {
PendingReviewPost {
content: text.to_string(),
}
Expand Down
26 changes: 13 additions & 13 deletions src/lib/smart_pointers.rs → src/smart_pointers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::cell::UnsafeCell;
// Further coverage of Cell, RefCell and Rc (https://youtu.be/8O0Nt9qY_vo):
// https://gist.github.com/jonhoo/7cfdfe581e5108b79c2a4e9fbde38de8

pub(crate) struct Cell<T> {
pub struct Cell<T> {
value: UnsafeCell<T>,
}

Expand All @@ -12,19 +12,19 @@ pub(crate) struct Cell<T> {
// https://doc.rust-lang.org/std/cell/struct.UnsafeCell.html#impl-Sync

impl<T> Cell<T> {
pub(crate) fn new(value: T) -> Self {
pub fn new(value: T) -> Self {
Cell {
value: UnsafeCell::new(value),
}
}

pub(crate) fn set(&self, value: T) {
pub fn set(&self, value: T) {
// SAFETY: we know no-one else is concurrently mutating self (because !Sync)
// SAFETY: we're not invalidating any references as we are not sharing any.
unsafe { *self.value.get() = value };
}

pub(crate) fn get(&self) -> T
pub fn get(&self) -> T
where
T: Copy,
{
Expand All @@ -35,49 +35,49 @@ impl<T> Cell<T> {
}

/// Contrived example storing a String as Vec<u8>
pub(crate) struct Message {
pub struct Message {
content: String,
bytes: Vec<u8>,
}

impl Message {
pub(crate) fn update(mut self, content: &str) -> Self {
pub fn update(mut self, content: &str) -> Self {
let bytes: Vec<u8> = content.to_string().as_bytes().to_vec();
self.bytes = bytes;

self
}

pub(crate) fn content_from_bytes(&self) -> Option<String> {
pub fn content_from_bytes(&self) -> Option<String> {
Some(String::from_utf8(self.bytes.clone()).ok()?)
}

pub(crate) fn content(&self) -> &String {
pub fn content(&self) -> &String {
&self.content
}

pub(crate) fn bytes(&self) -> &Vec<u8> {
pub fn bytes(&self) -> &Vec<u8> {
&self.bytes
}
}

pub(crate) struct MessageBuilder {
pub struct MessageBuilder {
content: String,
}

impl MessageBuilder {
pub(crate) fn new() -> Self {
pub fn new() -> Self {
Self {
content: String::default(),
}
}

pub(crate) fn content(mut self, content: &str) -> Self {
pub fn content(mut self, content: &str) -> Self {
self.content = content.to_string();
self
}

pub(crate) fn build(&self) -> Message {
pub fn build(&self) -> Message {
Message {
content: self.content.to_string(),
bytes: vec![0],
Expand Down
File renamed without changes.

0 comments on commit 5018e48

Please sign in to comment.