Skip to content

Commit

Permalink
refactor: move implementation to test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandervantrijffel committed Oct 21, 2024
1 parent 85bbef7 commit 6480376
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/graphemes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use unicode_segmentation::UnicodeSegmentation;

/// Trait extension example
pub trait Trim {
fn trim_count(&self, count: usize) -> String;
}
Expand Down
32 changes: 13 additions & 19 deletions src/mutate_in_closure.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
use std::cell::RefCell;

/// Use ``RefCell`` to mutate a variable in a closure
/// # Panics
///
pub fn mutate_in_closure() {
let my_string = RefCell::new("hello".to_string());
let mutate = |new_val| {
*my_string.borrow_mut() = new_val;
};
// mutate in closure
mutate("hej".to_string());
assert_eq!(*my_string.borrow(), "hej");

*my_string.borrow_mut() = "bonjour".to_string();
assert_eq!(*my_string.borrow(), "bonjour");
}

#[cfg(test)]
mod tests {
use std::cell::RefCell;
/// Use ``RefCell`` to mutate a variable in a closure
#[test]
fn test_mutate_in_closure() {
super::mutate_in_closure();
let my_string = RefCell::new("hello".to_string());
let mutate = |new_val| {
*my_string.borrow_mut() = new_val;
};
// mutate in closure
mutate("hej".to_string());
assert_eq!(*my_string.borrow(), "hej");

*my_string.borrow_mut() = "bonjour".to_string();
assert_eq!(*my_string.borrow(), "bonjour");
}
}
41 changes: 19 additions & 22 deletions src/vec_any.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::any::Any;

trait AsAny {
pub trait AsAny {
fn as_any(&self) -> &dyn Any;
}
impl<T: Any + Animal> AsAny for T {
Expand All @@ -13,8 +13,8 @@ pub trait Animal {
fn talk(&self);
}

struct Cat {}
struct Dog {
pub struct Cat {}
pub struct Dog {
pub name: String,
}

Expand All @@ -29,30 +29,27 @@ impl Animal for Dog {
}
}

trait AnyAnimal: Animal + AsAny {}
pub trait AnyAnimal: Animal + AsAny {}
impl<T: Animal + AsAny> AnyAnimal for T {}
type BoxedAnimal = Box<dyn AnyAnimal>;
pub type BoxedAnimal = Box<dyn AnyAnimal>;

/// Demonstrates creating a vector of common Animal trait objects,
/// then downcasting to a concrete Dog type instance to be able to access its fields.
/// # Panics
///
pub fn downcast_concrete_type_instance_from_trait_object() {
let c = Cat {};
let d = Dog { name: "Fido".to_string() };
#[cfg(test)]
mod tests {
use super::*;

let the_zoo = [Box::new(c) as BoxedAnimal, Box::new(d) as BoxedAnimal];
/// Demonstrates creating a vector of common Animal trait objects,
/// then downcasting to a concrete Dog type instance to be able to access its fields.
#[test]
fn test_downcast_concrete_type_instance_from_trait_object() {
let c = Cat {};
let d = Dog { name: "Fido".to_string() };

the_zoo.iter().for_each(|a| a.talk());
let the_zoo = [Box::new(c) as BoxedAnimal, Box::new(d) as BoxedAnimal];

let x = &the_zoo[1];
let dog = x.as_any().downcast_ref::<Dog>().unwrap();
assert!(dog.name == "Fido");
}
the_zoo.iter().for_each(|a| a.talk());

mod tests {
#[test]
fn test_cats_and_dogs() {
super::downcast_concrete_type_instance_from_trait_object();
let x = &the_zoo[1];
let dog = x.as_any().downcast_ref::<Dog>().unwrap();
assert!(dog.name == "Fido");
}
}

0 comments on commit 6480376

Please sign in to comment.