1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
use std::ptr::null_mut;
use anyhow::{bail, Result};
use crate::device_error_raw;
pub struct Device {
    pub(crate) handle: embree4_sys::RTCDevice,
}
impl Device {
    /// Constructs a new `Device` using the provided configuration string.
    ///
    /// # Arguments
    /// * `config` - A string representing the configuration for the device. Can be an empty string.
    ///              See [rtcNewDevice](https://github.com/embree/embree/blob/master/doc/src/api/rtcNewDevice.md) for valid configuration values.
    ///
    /// # Returns
    /// A `Result` containing the created `Device` if successful, or an error if the device creation fails.
    ///
    /// # Examples
    /// ```
    /// use embree4_rs::Device;
    ///
    /// let device = Device::try_new(Some("verbose=1")).unwrap();
    /// // Use the device...
    /// ```
    pub fn try_new(config: Option<&str>) -> Result<Self> {
        let handle = match config {
            None => unsafe { embree4_sys::rtcNewDevice(null_mut()) },
            Some(config) => unsafe {
                embree4_sys::rtcNewDevice(config.as_bytes() as *const _ as _)
            },
        };
        if handle.is_null() {
            let error = device_error_raw(null_mut());
            bail!("Failed to create device: {:?}", error);
        }
        Ok(Device { handle })
    }
    /// Returns the error code associated with the device, if any.
    ///
    /// # Returns
    /// `Some(error_code)` if there is an error associated with the device, otherwise `None`.
    pub fn error(&self) -> Option<embree4_sys::RTCError> {
        device_error_raw(self.handle)
    }
}
impl Drop for Device {
    fn drop(&mut self) {
        unsafe {
            embree4_sys::rtcReleaseDevice(self.handle);
        }
    }
}
#[test]
fn try_new_valid_config() {
    let ok_device = Device::try_new(Some("verbose=0"));
    assert!(ok_device.is_ok());
}
#[test]
fn try_new_invalid_config() {
    let err_device = Device::try_new(Some("verbose=bruh"));
    assert!(err_device.is_err());
}
#[test]
fn try_new_no_config() {
    let ok_device = Device::try_new(None);
    assert!(ok_device.is_ok());
}