mirror of
				https://github.com/edera-dev/krata.git
				synced 2025-11-03 23:29:39 +00:00 
			
		
		
		
	xenevtchn: convert to using thiserror
This commit is contained in:
		@ -5,6 +5,7 @@ edition = "2021"
 | 
			
		||||
resolver = "2"
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
thiserror = { workspace = true }
 | 
			
		||||
log = { workspace = true }
 | 
			
		||||
 | 
			
		||||
[dependencies.nix]
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,7 @@
 | 
			
		||||
use xenevtchn::{EventChannel, EventChannelError};
 | 
			
		||||
use xenevtchn::error::Result;
 | 
			
		||||
use xenevtchn::EventChannel;
 | 
			
		||||
 | 
			
		||||
fn main() -> Result<(), EventChannelError> {
 | 
			
		||||
fn main() -> Result<()> {
 | 
			
		||||
    let mut channel = EventChannel::open()?;
 | 
			
		||||
    println!("Channel opened.");
 | 
			
		||||
    let port = channel.bind_unbound_port(1)?;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										11
									
								
								xen/xenevtchn/src/error.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								xen/xenevtchn/src/error.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
			
		||||
use std::io;
 | 
			
		||||
 | 
			
		||||
#[derive(thiserror::Error, Debug)]
 | 
			
		||||
pub enum Error {
 | 
			
		||||
    #[error("kernel error")]
 | 
			
		||||
    Kernel(#[from] nix::errno::Errno),
 | 
			
		||||
    #[error("io issue encountered")]
 | 
			
		||||
    Io(#[from] io::Error),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub type Result<T> = std::result::Result<T, Error>;
 | 
			
		||||
@ -1,9 +1,9 @@
 | 
			
		||||
pub mod error;
 | 
			
		||||
pub mod sys;
 | 
			
		||||
 | 
			
		||||
use crate::error::Result;
 | 
			
		||||
use crate::sys::{BindInterdomain, BindUnboundPort, BindVirq, Notify, UnbindPort};
 | 
			
		||||
use nix::errno::Errno;
 | 
			
		||||
use std::error::Error;
 | 
			
		||||
use std::fmt::{Display, Formatter};
 | 
			
		||||
 | 
			
		||||
use std::fs::{File, OpenOptions};
 | 
			
		||||
use std::os::fd::AsRawFd;
 | 
			
		||||
 | 
			
		||||
@ -11,45 +11,8 @@ pub struct EventChannel {
 | 
			
		||||
    pub handle: File,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct EventChannelError {
 | 
			
		||||
    message: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl EventChannelError {
 | 
			
		||||
    pub fn new(msg: &str) -> EventChannelError {
 | 
			
		||||
        EventChannelError {
 | 
			
		||||
            message: msg.to_string(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Display for EventChannelError {
 | 
			
		||||
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
 | 
			
		||||
        write!(f, "{}", self.message)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Error for EventChannelError {
 | 
			
		||||
    fn description(&self) -> &str {
 | 
			
		||||
        &self.message
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<std::io::Error> for EventChannelError {
 | 
			
		||||
    fn from(value: std::io::Error) -> Self {
 | 
			
		||||
        EventChannelError::new(value.to_string().as_str())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl From<Errno> for EventChannelError {
 | 
			
		||||
    fn from(value: Errno) -> Self {
 | 
			
		||||
        EventChannelError::new(value.to_string().as_str())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl EventChannel {
 | 
			
		||||
    pub fn open() -> Result<EventChannel, EventChannelError> {
 | 
			
		||||
    pub fn open() -> Result<EventChannel> {
 | 
			
		||||
        let file = OpenOptions::new()
 | 
			
		||||
            .read(true)
 | 
			
		||||
            .write(true)
 | 
			
		||||
@ -57,14 +20,14 @@ impl EventChannel {
 | 
			
		||||
        Ok(EventChannel { handle: file })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn bind_virq(&mut self, virq: u32) -> Result<u32, EventChannelError> {
 | 
			
		||||
    pub fn bind_virq(&mut self, virq: u32) -> Result<u32> {
 | 
			
		||||
        unsafe {
 | 
			
		||||
            let mut request = BindVirq { virq };
 | 
			
		||||
            Ok(sys::bind_virq(self.handle.as_raw_fd(), &mut request)? as u32)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn bind_interdomain(&mut self, domid: u32, port: u32) -> Result<u32, EventChannelError> {
 | 
			
		||||
    pub fn bind_interdomain(&mut self, domid: u32, port: u32) -> Result<u32> {
 | 
			
		||||
        unsafe {
 | 
			
		||||
            let mut request = BindInterdomain {
 | 
			
		||||
                remote_domain: domid,
 | 
			
		||||
@ -74,7 +37,7 @@ impl EventChannel {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn bind_unbound_port(&mut self, domid: u32) -> Result<u32, EventChannelError> {
 | 
			
		||||
    pub fn bind_unbound_port(&mut self, domid: u32) -> Result<u32> {
 | 
			
		||||
        unsafe {
 | 
			
		||||
            let mut request = BindUnboundPort {
 | 
			
		||||
                remote_domain: domid,
 | 
			
		||||
@ -83,21 +46,21 @@ impl EventChannel {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn unbind(&mut self, port: u32) -> Result<u32, EventChannelError> {
 | 
			
		||||
    pub fn unbind(&mut self, port: u32) -> Result<u32> {
 | 
			
		||||
        unsafe {
 | 
			
		||||
            let mut request = UnbindPort { port };
 | 
			
		||||
            Ok(sys::unbind(self.handle.as_raw_fd(), &mut request)? as u32)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn notify(&mut self, port: u32) -> Result<u32, EventChannelError> {
 | 
			
		||||
    pub fn notify(&mut self, port: u32) -> Result<u32> {
 | 
			
		||||
        unsafe {
 | 
			
		||||
            let mut request = Notify { port };
 | 
			
		||||
            Ok(sys::notify(self.handle.as_raw_fd(), &mut request)? as u32)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn reset(&mut self) -> Result<u32, EventChannelError> {
 | 
			
		||||
    pub fn reset(&mut self) -> Result<u32> {
 | 
			
		||||
        unsafe { Ok(sys::reset(self.handle.as_raw_fd())? as u32) }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user