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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//! Scalar (number-like) atoms
//!
//! There are several scalar atoms:
//! * `i32`
//! * `i64`
//! * `f32`
//! * `f64`
//! * `bool`
//! * `URID`
//!
//! They all have in common that they are statically sized (which is something special among atoms)
//! and that they can be written in one piece; Once they are initialized, they are completed and
//! need no further amendments. Therefore, their behaviour is abstracted to another trait,
//! [`ScalarAtomBody`](trait.ScalarAtomBody.html), which features a standard implementation of
//! [`AtomBody`](../atom/trait.AtomBody.html) for every type that implements it. Therefore, writing
//! and reading scalar atoms is pretty straight foreward:
//!
//!     extern crate lv2rs_atom as atom;
//!     extern crate lv2rs_urid as urid;
//!
//!     use atom::prelude::*;
//!     use atom::ports::*;
//!     use urid::{CachedMap, debug::DebugMap};
//!     use std::ffi::CStr;
//!
//!     pub struct Plugin {
//!         in_port: AtomInputPort<f32>,
//!         out_port: AtomOutputPort<f32>,
//!         urids: CachedMap,
//!     }
//!
//!     impl Plugin {
//!         /// Simulated `run` method.
//!         fn run(&mut self) {
//!             // Writing.
//!             unsafe { self.out_port.write_atom_body(&42.0f32, &mut self.urids) }.unwrap();
//!
//!             // Reading.
//!             let float = unsafe { self.in_port.get_atom_body(&mut self.urids) }.unwrap();
//!             assert_eq!(42.0, *float);
//!         }
//!     }
//!
//!     // Getting a debug URID map.
//!     let mut debug_map = DebugMap::new();
//!     let mut urids = unsafe {debug_map.create_cached_map()};
//!
//!     // Creating the plugin.
//!     let mut plugin = Plugin {
//!         in_port: AtomInputPort::new(),
//!         out_port: AtomOutputPort::new(),
//!         urids: urids,
//!     };
//!
//!     // Creating the atom space.
//!     let mut atom_space = vec![0u8; 256];
//!     let atom = unsafe { (atom_space.as_mut_ptr() as *mut Atom).as_mut() }.unwrap();
//!     *(atom.mut_size()) = 256 - 8;
//!
//!     // Connecting the ports.
//!     plugin.in_port.connect_port(atom as &Atom);
//!     plugin.out_port.connect_port(atom);
//!
//!     // Calling `run`.
//!     plugin.run();
use crate::atom::*;
use crate::frame::{WritingFrame, WritingFrameExt};
use crate::uris;
use std::ffi::CStr;

/// Abstraction over scalar (number-like) atoms.
///
/// See the [module documentation](index.html) for more information.
pub trait ScalarAtomBody {
    fn get_uri() -> &'static CStr;
}

impl<T> AtomBody for T
where
    T: 'static + Sized + ScalarAtomBody,
{
    type InitializationParameter = Self;

    fn get_uri() -> &'static CStr {
        T::get_uri()
    }

    unsafe fn initialize_body<'a, W>(
        writer: &mut W,
        parameter: &Self,
        _urids: &mut urid::CachedMap,
    ) -> Result<(), ()>
    where
        W: WritingFrame<'a> + WritingFrameExt<'a, Self>,
    {
        writer.write_sized(parameter)?;
        Ok(())
    }

    fn create_ref<'a>(raw_body: &'a [u8]) -> Result<&'a Self, ()> {
        if raw_body.len() == std::mem::size_of::<Self>() {
            let ptr = raw_body.as_ptr() as *const Self;
            Ok(unsafe { ptr.as_ref() }.unwrap())
        } else {
            Err(())
        }
    }
}

impl ScalarAtomBody for i32 {
    fn get_uri() -> &'static CStr {
        unsafe { CStr::from_bytes_with_nul_unchecked(uris::INT_TYPE_URI) }
    }
}

impl ScalarAtomBody for i64 {
    fn get_uri() -> &'static CStr {
        unsafe { CStr::from_bytes_with_nul_unchecked(uris::LONG_TYPE_URI) }
    }
}

impl ScalarAtomBody for f32 {
    fn get_uri() -> &'static CStr {
        unsafe { CStr::from_bytes_with_nul_unchecked(uris::FLOAT_TYPE_URI) }
    }
}

impl ScalarAtomBody for f64 {
    fn get_uri() -> &'static CStr {
        unsafe { CStr::from_bytes_with_nul_unchecked(uris::DOUBLE_TYPE_URI) }
    }
}

pub use urid::URID;

impl ScalarAtomBody for URID {
    fn get_uri() -> &'static CStr {
        unsafe { CStr::from_bytes_with_nul_unchecked(uris::URID_TYPE_URI) }
    }
}

impl ScalarAtomBody for bool {
    fn get_uri() -> &'static CStr {
        unsafe { CStr::from_bytes_with_nul_unchecked(uris::BOOL_TYPE_URI) }
    }
}