Skip to content

Commit 5018e48

Browse files
committed
FIX: Make sure lib.rs is implemented correctly
1 parent bb093b4 commit 5018e48

File tree

10 files changed

+50
-48
lines changed

10 files changed

+50
-48
lines changed

src/lib/builder.rs renamed to src/builder.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
#[derive(Debug)]
2-
pub(crate) struct TaskManager {
2+
pub struct TaskManager {
33
state: String,
44
count: usize,
55
}
66

77
impl TaskManager {
88
/// Get task count
9-
pub(crate) fn count(&self) -> &usize {
9+
pub fn count(&self) -> &usize {
1010
&self.count
1111
}
1212
}
1313

1414
#[derive(Default)]
15-
pub(crate) struct TaskManagerBuilder {
15+
pub struct TaskManagerBuilder {
1616
state: String,
1717
count: usize,
1818
}
1919

2020
impl TaskManagerBuilder {
2121
/// Creates a new TaskManagerBuilder
22-
pub(crate) fn new() -> Self {
22+
pub fn new() -> Self {
2323
Self {
2424
state: "initialized".to_string(),
2525
count: 0,
2626
}
2727
}
2828

2929
/// Sets the task count
30-
pub(crate) fn count(mut self, value: usize) -> Self {
30+
pub fn count(mut self, value: usize) -> Self {
3131
self.count = value;
3232
self
3333
}
3434

3535
/// Creates a new TaskManager
36-
pub(crate) fn build(self) -> TaskManager {
36+
pub fn build(self) -> TaskManager {
3737
TaskManager {
3838
state: self.state,
3939
count: self.count,

src/lib/cli.rs renamed to src/cli.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use std::str::FromStr;
55
/// Program to run rust tutorials
66
#[derive(Parser, Debug)]
77
#[clap(author, version, about, long_about = None)]
8-
pub(crate) struct Args {
8+
pub struct Args {
99
#[clap(subcommand)]
10-
pub(crate) command: Option<Commands>,
10+
pub command: Option<Commands>,
1111
}
1212

1313
#[derive(Subcommand, Debug)]
14-
pub(crate) enum Commands {
14+
pub enum Commands {
1515
/// Tutorial on dynamic dispatch
1616
Dispatch,
1717

@@ -31,6 +31,6 @@ pub(crate) enum Commands {
3131
Conversion,
3232
}
3333

34-
pub(crate) fn runner<T>(mut mk: impl FnMut() -> Result<T>) -> Result<T> {
34+
pub fn runner<T>(mut mk: impl FnMut() -> Result<T>) -> Result<T> {
3535
mk()
3636
}

src/lib/conversion/mod.rs renamed to src/conversion/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ pub fn lesson_1() -> Result<()> {
1616
let resp2 = u16::from(value);
1717
assert_eq!(resp, resp2);
1818

19+
let formal = <u8 as Into<u16>>::into(value);
20+
let shorter = value as u16;
21+
1922
Ok(())
2023
}
2124

src/lib/dispatch.rs renamed to src/dispatch.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// Say hello in Norwegian
2-
pub(crate) trait Hei {
2+
pub trait Hei {
33
fn hei(&self);
44

55
fn weird(&self);
@@ -37,23 +37,23 @@ impl Hei for String {
3737
}
3838
}
3939

40-
pub(crate) fn say_hei(s: &dyn Hei) {
40+
pub fn say_hei(s: &dyn Hei) {
4141
s.hei()
4242
}
4343

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

48-
pub(crate) fn strlen2(s: String) -> usize {
48+
pub fn strlen2(s: String) -> usize {
4949
s.len()
5050
}
5151

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

57-
pub(crate) fn strlen_dyn(s: &dyn AsRef<str>) -> usize {
57+
pub fn strlen_dyn(s: &dyn AsRef<str>) -> usize {
5858
s.as_ref().len()
5959
}
File renamed without changes.

src/main.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88

99
use anyhow::{Error, Result};
1010
use clap::Parser;
11-
use lib::cli::{runner, Args, Commands};
12-
use lib::{builder::TaskManagerBuilder, dispatch::*, oop_pattern::*, smart_pointers::*, traits};
1311
use log::{debug, info};
1412
use std::io::Read;
1513
use std::sync::Arc;
16-
17-
pub mod lib;
18-
// pub mod sandbox;
14+
use tutorials::cli::{runner, Args, Commands};
15+
use tutorials::{
16+
builder::TaskManagerBuilder, dispatch::*, oop_pattern::*, smart_pointers::*, traits,
17+
};
1918

2019
fn main() -> Result<()> {
2120
env_logger::init();
@@ -131,7 +130,7 @@ fn main() -> Result<()> {
131130
Some(Commands::Conversion) => runner(|| {
132131
info!("Tutorial: Conversion\n");
133132

134-
lib::conversion::runner()?;
133+
tutorials::conversion::runner()?;
135134

136135
Ok(())
137136
})?,
File renamed without changes.

src/lib/oop_pattern.rs renamed to src/oop_pattern.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
1-
pub(crate) struct Post {
1+
pub struct Post {
22
content: String,
33
}
44

5-
pub(crate) struct DraftPost {
5+
pub struct DraftPost {
66
content: String,
77
}
88

99
impl Post {
10-
pub(crate) fn new() -> DraftPost {
10+
pub fn new() -> DraftPost {
1111
DraftPost {
1212
content: String::new(),
1313
}
1414
}
1515

16-
pub(crate) fn content(&self) -> &str {
16+
pub fn content(&self) -> &str {
1717
&self.content
1818
}
1919
}
2020

2121
impl DraftPost {
22-
pub(crate) fn add_text(&mut self, text: &str) {
22+
pub fn add_text(&mut self, text: &str) {
2323
self.content.push_str(text);
2424
}
2525

26-
pub(crate) fn request_review(self) -> PendingReviewPost {
26+
pub fn request_review(self) -> PendingReviewPost {
2727
PendingReviewPost {
2828
content: self.content,
2929
}
3030
}
3131
}
3232

33-
pub(crate) struct PendingReviewPost {
33+
pub struct PendingReviewPost {
3434
content: String,
3535
}
3636

3737
impl PendingReviewPost {
38-
pub(crate) fn review(&self) -> &String {
38+
pub fn review(&self) -> &String {
3939
&self.content
4040
}
4141

42-
pub(crate) fn approve(self) -> Post {
42+
pub fn approve(self) -> Post {
4343
Post {
4444
content: self.content,
4545
}
4646
}
4747

48-
pub(crate) fn reject(self, changes: &str) -> RequestChangesPost {
48+
pub fn reject(self, changes: &str) -> RequestChangesPost {
4949
RequestChangesPost {
5050
content: self.content,
5151
changes: changes.to_string(),
5252
}
5353
}
5454
}
5555

56-
pub(crate) struct RequestChangesPost {
56+
pub struct RequestChangesPost {
5757
content: String,
5858
changes: String,
5959
}
6060

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

66-
pub(crate) fn replace_text(&mut self, text: &str) -> PendingReviewPost {
66+
pub fn replace_text(&mut self, text: &str) -> PendingReviewPost {
6767
PendingReviewPost {
6868
content: text.to_string(),
6969
}

src/lib/smart_pointers.rs renamed to src/smart_pointers.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::cell::UnsafeCell;
33
// Further coverage of Cell, RefCell and Rc (https://youtu.be/8O0Nt9qY_vo):
44
// https://gist.github.com/jonhoo/7cfdfe581e5108b79c2a4e9fbde38de8
55

6-
pub(crate) struct Cell<T> {
6+
pub struct Cell<T> {
77
value: UnsafeCell<T>,
88
}
99

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

1414
impl<T> Cell<T> {
15-
pub(crate) fn new(value: T) -> Self {
15+
pub fn new(value: T) -> Self {
1616
Cell {
1717
value: UnsafeCell::new(value),
1818
}
1919
}
2020

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

27-
pub(crate) fn get(&self) -> T
27+
pub fn get(&self) -> T
2828
where
2929
T: Copy,
3030
{
@@ -35,49 +35,49 @@ impl<T> Cell<T> {
3535
}
3636

3737
/// Contrived example storing a String as Vec<u8>
38-
pub(crate) struct Message {
38+
pub struct Message {
3939
content: String,
4040
bytes: Vec<u8>,
4141
}
4242

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

4848
self
4949
}
5050

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

55-
pub(crate) fn content(&self) -> &String {
55+
pub fn content(&self) -> &String {
5656
&self.content
5757
}
5858

59-
pub(crate) fn bytes(&self) -> &Vec<u8> {
59+
pub fn bytes(&self) -> &Vec<u8> {
6060
&self.bytes
6161
}
6262
}
6363

64-
pub(crate) struct MessageBuilder {
64+
pub struct MessageBuilder {
6565
content: String,
6666
}
6767

6868
impl MessageBuilder {
69-
pub(crate) fn new() -> Self {
69+
pub fn new() -> Self {
7070
Self {
7171
content: String::default(),
7272
}
7373
}
7474

75-
pub(crate) fn content(mut self, content: &str) -> Self {
75+
pub fn content(mut self, content: &str) -> Self {
7676
self.content = content.to_string();
7777
self
7878
}
7979

80-
pub(crate) fn build(&self) -> Message {
80+
pub fn build(&self) -> Message {
8181
Message {
8282
content: self.content.to_string(),
8383
bytes: vec![0],
File renamed without changes.

0 commit comments

Comments
 (0)