Struct Stack

Source
pub struct Stack<Val = Value>
where Val: Debug,
{ /* private fields */ }
Expand description

An ordered sequence of values that can be operated in a LIFO-alike way.

Every Stack actually comprises two sequences of values: the main sub-stack and the alt sub-stack.

As its name indicates, the main sub-stack is the one you operate by default. That is, the alt sub-stack cannot be operated directly — you can only move values between both sub-stacks with the pop_into_alt and push_from_alt methods. The alt sub-stack is therefore limited for usage as a sort of clipboard for values.

Implementations§

Source§

impl<Val> Stack<Val>
where Val: Debug,

Source

pub fn length(&self) -> usize

Returns the number of values in the main sub-stack, also referred to as its ‘length’.

§Examples
use scriptful::prelude::*;
use scriptful::core::value::Value::*;

let mut stack = Stack::default();
assert_eq!(stack.length(), 0);

stack.push(Integer(i128::default()));
assert_eq!(stack.length(), 1);

stack.pop();
assert_eq!(stack.length(), 0);
Source

pub fn pop(&mut self) -> Option<Val>

Removes the topmost value in the main sub-stack and returns it.

§Examples
use scriptful::prelude::*;
use scriptful::core::value::Value::*;

let value = Integer(i128::default());
let mut stack = Stack::default();
stack.push(value.clone());
let popped = stack.pop();

assert_eq!(value, popped);
Source

pub fn pop_into_alt(&mut self)

Similar to pop, but instead of returning the popped value, it pushes it to the alt sub-stack.

§Panics

Panics if there are no values left in the main stack.

Source

pub fn push(&mut self, item: Val)

Puts a value on top of the stack.

§Examples
use scriptful::prelude::*;
use scriptful::core::value::Value::*;

let value = Integer(i128::default());
let mut stack = Stack::default();
stack.push(value.clone());
let topmost = stack.topmost();

assert_eq!(topmost, Some(&value));
Source

pub fn push_from_alt(&mut self)

Similar to push, but instead of receiving the value to be pushed as an argument, it pops it from the alt sub-stack.

Source

pub fn topmost(&self) -> Option<&Val>

Returns a reference to the last value in the main sub-stack.

§Examples
use scriptful::prelude::*;
use scriptful::core::value::Value::*;

let value = Integer(i128::default());
let mut stack = Stack::default();
stack.push(value.clone());
let topmost = stack.topmost();

assert_eq!(topmost, Some(&value));

Trait Implementations§

Source§

impl<Val> Debug for Stack<Val>
where Val: Debug + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Val> Default for Stack<Val>
where Val: Debug,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<Val> Freeze for Stack<Val>

§

impl<Val> RefUnwindSafe for Stack<Val>
where Val: RefUnwindSafe,

§

impl<Val> Send for Stack<Val>
where Val: Send,

§

impl<Val> Sync for Stack<Val>
where Val: Sync,

§

impl<Val> Unpin for Stack<Val>
where Val: Unpin,

§

impl<Val> UnwindSafe for Stack<Val>
where Val: UnwindSafe,

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.