<commit_before>//! Implementation of the Xenstore protocol

use core::result;
use core::str::FromStr;

use ffi::CString;

use cell::GlobalCell;

use self::imp::XenStoreImpl;

use hal::xen::defs::{XenstoreInterface, EvtchnPort};

pub use self::transaction::Transaction;

static STORE: GlobalCell<XenStoreImpl> = GlobalCell::new();

mod imp;
mod transaction;

/// Result of an operation within the Xen Store
pub type Result<T> = result::Result<T, Error>;

#[derive(Debug)]
/// Error code that can be returned by a Xen Store operation
pub enum Error {
    /// Unknown error happened
    Unknown,
    /// A type conversion error
    Conversion,
    /// Invalid argument
    Inval,
    /// Permission denied
    Acces,
    /// File exists
    Exist,
    /// Is a directory
    Isdir,
    /// No such file or directory
    Noent,
    /// Out of memory
    Nomem,
    /// No space left on device
    Nospc,
    /// I/O error
    Io,
    /// Directory not empty
    Notempty,
    /// Function not implemented
    Nosys,
    /// Read-only filesystem
    Rofs,
    /// Device or resource is busy
    Busy,
    /// Try again
    Again,
    /// Transport endpoint is already connected
    Isconn,
}

/// Main interface of the Xen Store
pub struct XenStore;

impl XenStore {
    #[doc(hidden)]
    pub unsafe fn init(interface: *mut XenstoreInterface, port: EvtchnPort) {
        STORE.set(XenStoreImpl::new(interface, port));
    }

    #[doc(hidden)]
    pub unsafe fn init_event() {
        STORE.as_mut().init_event();
    }

    /// Start a new transaction within the Xen Store
    pub fn start_transaction<'a>() -> Result<Transaction<'a>> {
        Transaction::new(STORE.as_mut())
    }

    /// Read a value from the xen store and convert it
    pub fn read_value<T: FromStr>(path: CString) -> Result<T> {
        let mut t = try!(Self::start_transaction());

        let val = try!(t.read(path));

        try!(t.end());

        T::from_str(val.as_str()).or_else(|_| Err(Error::Conversion))
    }
}
<commit_msg>xen/store: impl From<Error> for ()<commit_after>//! Implementation of the Xenstore protocol

use core::result;
use core::str::FromStr;

use ffi::CString;

use cell::GlobalCell;

use self::imp::XenStoreImpl;

use hal::xen::defs::{XenstoreInterface, EvtchnPort};

pub use self::transaction::Transaction;

static STORE: GlobalCell<XenStoreImpl> = GlobalCell::new();

mod imp;
mod transaction;

/// Result of an operation within the Xen Store
pub type Result<T> = result::Result<T, Error>;

#[derive(Debug)]
/// Error code that can be returned by a Xen Store operation
pub enum Error {
    /// Unknown error happened
    Unknown,
    /// A type conversion error
    Conversion,
    /// Invalid argument
    Inval,
    /// Permission denied
    Acces,
    /// File exists
    Exist,
    /// Is a directory
    Isdir,
    /// No such file or directory
    Noent,
    /// Out of memory
    Nomem,
    /// No space left on device
    Nospc,
    /// I/O error
    Io,
    /// Directory not empty
    Notempty,
    /// Function not implemented
    Nosys,
    /// Read-only filesystem
    Rofs,
    /// Device or resource is busy
    Busy,
    /// Try again
    Again,
    /// Transport endpoint is already connected
    Isconn,
}

/// Useful for try!
impl From<Error> for () {
    #[inline]
    fn from(_: Error) -> Self {
        ()
    }
}

/// Main interface of the Xen Store
pub struct XenStore;

impl XenStore {
    #[doc(hidden)]
    pub unsafe fn init(interface: *mut XenstoreInterface, port: EvtchnPort) {
        STORE.set(XenStoreImpl::new(interface, port));
    }

    #[doc(hidden)]
    pub unsafe fn init_event() {
        STORE.as_mut().init_event();
    }

    /// Start a new transaction within the Xen Store
    pub fn start_transaction<'a>() -> Result<Transaction<'a>> {
        Transaction::new(STORE.as_mut())
    }

    /// Read a value from the xen store and convert it
    pub fn read_value<T: FromStr>(path: CString) -> Result<T> {
        let mut t = try!(Self::start_transaction());

        let val = try!(t.read(path));

        try!(t.end());

        T::from_str(val.as_str()).or_else(|_| Err(Error::Conversion))
    }
}

{'fraction_non_alphanumeric': 0.13304410701373826, 'fraction_numerical': 0.0, 'mean_word_length': 2.5591766723842198, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '889346', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1287, 'n_words': 399}