irid-std 0.2.3

A replacement for std when running without a filesystem on the irid kernel
Documentation
use irid_syscall::{ffi::io::{IO_LENGTH, IO_TRY_ACQUIRE}, io::{FileDescriptor, OpenFlags, close, open}};

use crate::{io, path::Path};

#[derive(Debug)]
pub struct SemaphoreFile {
    desc: FileDescriptor
}

impl SemaphoreFile {
    pub fn open_read(path: impl AsRef<Path>) -> Result<Self, io::Error> {
        Ok(SemaphoreFile { 
            desc: open(path.as_ref().as_str(), OpenFlags::READ | OpenFlags::SEMAPHORE | OpenFlags::CREATE)?
        })
    }
    
    pub fn open(path: impl AsRef<Path>) -> Result<Self, io::Error> {
        Ok(SemaphoreFile { 
            desc: open(path.as_ref().as_str(), OpenFlags::READ | OpenFlags::WRITE | OpenFlags::SEMAPHORE | OpenFlags::CREATE)?
        })
    }
    
    pub fn try_acquire(&self) -> Result<bool, io::Error> {
        irid_syscall::io::get_file_config(self.desc, IO_TRY_ACQUIRE)?
    }

    pub fn acquire(&self) -> Result<(), io::Error> {
        irid_syscall::io::read(self.desc, &mut [0])?;

        Ok(())
    }
    
    pub fn release(&self) -> Result<(), io::Error> {
        irid_syscall::io::write(self.desc, &[1])?;

        Ok(())
    }

    pub fn set_permits(&self, value: usize) -> Result<(), io::Error> {
        irid_syscall::io::set_file_config::<u64>(self.desc, IO_LENGTH, value as u64)?;

        Ok(())
    }
}

impl Drop for SemaphoreFile {
    fn drop(&mut self) {
        close(self.desc).expect("failed to close semaphore file")
    }
}