Skip to content
Snippets Groups Projects
Commit 7b65860f authored by iliya.saroukha's avatar iliya.saroukha
Browse files

working on runtime sized stack

parent 4cdcf1da
Branches master
No related tags found
No related merge requests found
/target/
[package]
name = "unsafe_stack"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
use unsafe_stack::{Stack, StackError};
mod unsafe_stack;
fn main() -> Result<(), StackError> {
let mut new_stack: Stack<f64> = Stack::new(10).unwrap();
for i in 0..new_stack.capacity {
unsafe {
new_stack.push(i as f64).unwrap();
}
}
println!("{}", new_stack);
Ok(())
}
#[derive(Debug)]
pub enum StackError {
AllocationFailed,
StackFull,
StackEmpty,
}
#[derive(Debug)]
pub struct Stack<T> {
pointer: *mut T,
length: usize,
pub capacity: usize,
}
impl<T: Copy + std::fmt::Display> std::fmt::Display for Stack<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Ok(for i in 0..self.capacity {
unsafe {
write!(f, "{} ", *self.pointer.offset(i as isize) as T)?;
}
})
}
}
#[allow(dead_code)]
impl<T: Copy> Stack<T> {
pub fn new(capacity: usize) -> Result<Self, StackError> {
unsafe {
let layout = std::alloc::Layout::array::<T>(capacity);
if let Ok(val) = layout {
let ptr = std::alloc::alloc(val);
if ptr.is_null() {
return Err(StackError::AllocationFailed);
}
Ok(Self {
pointer: ptr as *mut T,
length: 0,
capacity,
})
} else {
Err(StackError::AllocationFailed)
}
}
}
pub fn is_full(&self) -> bool {
self.length == self.capacity
}
pub fn is_empty(&self) -> bool {
self.length == 0
}
pub unsafe fn push(&mut self, value: T) -> Result<(), StackError> {
match self.is_full() {
true => Err(StackError::StackFull),
false => unsafe {
let location = self.pointer.offset(self.length as isize);
*location = value;
self.length += 1;
Ok(())
},
}
}
pub unsafe fn pop(&mut self) -> Result<T, StackError> {
match self.is_empty() {
true => Err(StackError::StackEmpty),
false => unsafe {
let location = self.pointer.offset(self.length as isize - 1);
self.length -= 1;
Ok(*location)
},
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment