[][src]Module lv2rs_atom::string

ASCII string.

This module contains the AtomString, an atom representing standard ASCII strings.

Atom strings can only be written once: The write_atom_body call expects a CStr from which it can copy the data and after that call, the string can't be modified.

An example:

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<AtomString>,
    out_port: AtomOutputPort<AtomString>,
    urids: CachedMap,
}

impl Plugin {
    /// Simulated `run` method.
    fn run(&mut self) {
        let message: &str = "Hello World!\0";
        let c_message = CStr::from_bytes_with_nul(message.as_bytes()).unwrap();

        // Writing.
        unsafe { self.out_port.write_atom_body(c_message, &mut self.urids).unwrap() };

        // Reading.
        let string = unsafe { self.in_port.get_atom_body(&mut self.urids) }.unwrap();
        let str = string.as_cstr().unwrap().to_str().unwrap();
        assert_eq!("Hello World!", str);
    }
}

// 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();

Type Definitions

AtomString

ASCII String.