rust_dsa

Struct AnyStack

Source
pub struct AnyStack(/* private fields */);
Expand description

A stack that can hold arbitrary types.

§Example

use rust_dsa::AnyStack;

// First, we create a new stack.
let mut stack = AnyStack::new();

// We can push elements of different types.
stack.push(4);
stack.push(true);
stack.push('a');
stack.push("str");

// We can check the top type.
assert!(stack.top_is::<&str>());
assert!(!stack.top_is::<char>());

// We can peek the top value.
assert_eq!(stack.peek::<&str>(), Some(&"str"));

// If we try to peek the wrong type, we get None.
assert_eq!(stack.peek::<bool>(), None);

// We can also pop values.
assert_eq!(stack.pop::<&str>(), Some("str"));
assert_eq!(stack.pop::<char>(), Some('a'));

// We can check the stack's length.
assert_eq!(stack.len(), 2);

// And clear the stack.
stack.clear();
assert!(stack.is_empty());

Implementations§

Source§

impl AnyStack

Source

pub fn new() -> AnyStack

Creates a new stack.

Source

pub fn push<T: Any>(&mut self, value: T)

Pushes a value onto the stack.

§Example
use rust_dsa::AnyStack;

let mut stack = AnyStack::new();
stack.push(5);

assert_eq!(stack.pop::<i32>(), Some(5));
assert_eq!(stack.pop::<i32>(), None);
Source

pub fn pop<T: Any>(&mut self) -> Option<T>

Tries to pop a value of type T from the stack.

§Example
use rust_dsa::AnyStack;

let mut stack = AnyStack::new();
stack.push('a');

assert_eq!(stack.pop::<i32>(), None);
assert_eq!(stack.pop::<char>(), Some('a'));
assert_eq!(stack.pop::<char>(), None);
Source

pub fn peek<T: Any>(&self) -> Option<&T>

Tries to peek a value of type T from the top of the stack.

§Example
use rust_dsa::AnyStack;

let mut stack = AnyStack::new();
stack.push(4);

assert_eq!(stack.peek::<i32>(), Some(&4));
assert_eq!(stack.peek::<char>(), None);
Source

pub fn top_is<T: Any>(&self) -> bool

Returns true if the top of the stack has type T.

This function returns false if the stack is empty.

§Example
use rust_dsa::AnyStack;

let mut stack = AnyStack::new();
stack.push('z');

assert!(stack.top_is::<char>());
assert!(!stack.top_is::<i32>());
Source

pub fn len(&self) -> usize

Returns the length of the stack.

§Example
use rust_dsa::AnyStack;

let mut stack = AnyStack::new();
stack.push(1);
stack.push(2.0);
stack.push("three");

assert_eq!(stack.len(), 3);
Source

pub fn is_empty(&self) -> bool

Returns true if the stack is empty.

Source

pub fn clear(&mut self)

Removes all values from the stack.

§Example
use rust_dsa::AnyStack;

let mut stack = AnyStack::new();
stack.push(1);
stack.push(false);
assert_eq!(stack.len(), 2);

stack.clear();

assert!(stack.is_empty());

Trait Implementations§

Source§

impl Default for AnyStack

Source§

fn default() -> AnyStack

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.