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
use std::convert::TryFrom;
use std::ffi::CStr;
use sys::*;
use error::{Result, Error};
use string::IntoCString;
use super::JlValue;
pub trait IntoSymbol {
fn into_symbol(self) -> Result<Symbol>;
}
jlvalues! {
pub struct Symbol(jl_sym_t);
}
impl Symbol {
pub fn with_name<S: IntoCString>(name: S) -> Result<Symbol> {
let name = name.into_cstring();
let raw = unsafe { jl_symbol(name.as_ptr()) };
Symbol::new(raw).map_err(|_| Error::InvalidSymbol)
}
}
impl IntoSymbol for Symbol {
fn into_symbol(self) -> Result<Symbol> {
Ok(self)
}
}
impl<S: IntoCString> IntoSymbol for S {
fn into_symbol(self) -> Result<Symbol> {
Symbol::with_name(self.into_cstring())
}
}
impl<'a> TryFrom<&'a Symbol> for String {
type Error = Error;
fn try_from(sym: &Symbol) -> Result<String> {
let raw = unsafe { jl_symbol_name(sym.lock()?) };
jl_catch!();
let cstr = unsafe { CStr::from_ptr(raw) };
let cstring = cstr.to_owned();
cstring.into_string().map_err(From::from)
}
}