Skip to content

Commit

Permalink
feat(rs-port2): support char with metacall value
Browse files Browse the repository at this point in the history
  • Loading branch information
hulxv committed Dec 2, 2024
1 parent dc991db commit 0246006
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
8 changes: 8 additions & 0 deletions source/ports/rs_port2/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ mod test {
let result = value.get_value().unwrap();
assert_eq!(result, str, "Failed in &str test");
println!("[&str] \"{result}\" == \"{str}\" => Passed",);

let ch = 'F';
let value = Value::<char>::new(ch).unwrap();
let result = value.get_value().unwrap();
assert_eq!(result, ch, "Failed in &str test");
println!("[char] \"{result}\" == \"{ch}\" => Passed",);
}

#[test]
Expand Down Expand Up @@ -126,4 +132,6 @@ mod test {
assert_eq!(result, float64);
println!("[i64] \"{result}\" == \"{float64}\" => Passed",);
}


}
39 changes: 33 additions & 6 deletions source/ports/rs_port2/src/value/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,34 @@ use std::{
};

use anyhow::Result;
use metacall_bindings::value::{create::metacall_value_create_string, metacall_value_to_string};
use metacall_bindings::value::{
create::{metacall_value_create_char, metacall_value_create_string},
metacall_value_to_char, metacall_value_to_string,
};

use crate::{check_null_ptr, impl_value_constructor};

use super::{Value, ValueError};

impl_value_constructor!(
value => {
let value = value.to_string();
let ptr = value.as_ptr() as *const c_char;
let ptr = unsafe {
metacall_value_create_string(ptr, value.len())
};

check_null_ptr!(ptr, ValueError::NullPointer);

Ok(Self {
ptr,
_phantom: PhantomData,
})
},
String,
&str
);

impl Value<&str> {
pub fn get_value<'a>(&self) -> Result<&'a str> {
let ptr = unsafe { metacall_value_to_string(self.ptr) };
Expand All @@ -36,10 +58,8 @@ impl Value<String> {

impl_value_constructor!(
value => {
let value = value.to_string();
let ptr = value.as_ptr() as *const c_char;
let ptr = unsafe {
metacall_value_create_string(ptr, value.len())
metacall_value_create_char(value as c_char)
};

check_null_ptr!(ptr, ValueError::NullPointer);
Expand All @@ -49,6 +69,13 @@ impl_value_constructor!(
_phantom: PhantomData,
})
},
String,
&str
char, i8
);

impl Value<char> {
pub fn get_value(&self) -> Result<char> {
let ptr = unsafe { metacall_value_to_char(self.ptr) };

Ok(ptr as u8 as char)
}
}

0 comments on commit 0246006

Please sign in to comment.