Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce DiplomatChar #366

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/ast/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ lazy_static! {
("f32", PrimitiveType::f32),
("f64", PrimitiveType::f64),
("bool", PrimitiveType::bool),
("char", PrimitiveType::char),
("DiplomatChar", PrimitiveType::char),
];
static ref STRING_TO_PRIMITIVE: HashMap<&'static str, PrimitiveType> =
PRIMITIVES_MAPPING.iter().cloned().collect();
Expand Down
2 changes: 1 addition & 1 deletion core/src/ast/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ mod tests {

}

pub fn do_thing2(opt: DiplomatResult<Option<char>, u8>) {
pub fn do_thing2(opt: DiplomatResult<Option<DiplomatChar>, u8>) {

}
pub fn do_thing2(opt: Option<u16>) {
Expand Down
4 changes: 2 additions & 2 deletions feature_tests/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ pub mod ffi {
}

impl OptionOpaqueChar {
pub fn assert_char(&self, ch: char) {
assert_eq!(ch, self.0)
pub fn assert_char(&self, ch: DiplomatChar) {
assert_eq!(ch, self.0 as u32)
}
}
}
6 changes: 3 additions & 3 deletions feature_tests/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub mod ffi {
c: u8,
d: u64,
e: i32,
f: char,
f: DiplomatChar,
g: MyEnum,
}

Expand Down Expand Up @@ -71,7 +71,7 @@ pub mod ffi {
c: 209,
d: 1234,
e: 5991,
f: '餐',
f: '餐' as DiplomatChar,
g: MyEnum::B,
}
}
Expand All @@ -86,7 +86,7 @@ pub mod ffi {
assert_eq!(self.c, 209);
assert_eq!(self.d, 1234);
assert_eq!(self.e, 5991);
assert_eq!(self.f, '餐');
assert_eq!(self.f, '餐' as DiplomatChar);
assert_eq!(self.g, MyEnum::B);
}
}
Expand Down
15 changes: 2 additions & 13 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ fn gen_params_at_boundary(param: &ast::Param, expanded_params: &mut Vec<FnArg>)
),
}));
}
ast::TypeName::Primitive(ast::PrimitiveType::char) => gen_params_at_boundary(
&{
let mut p = param.clone();
p.ty = ast::TypeName::Primitive(ast::PrimitiveType::u32);
p
},
expanded_params,
),
o => {
expanded_params.push(FnArg::Typed(PatType {
attrs: vec![],
Expand Down Expand Up @@ -118,11 +110,6 @@ fn gen_params_invocation(param: &ast::Param, expanded_params: &mut Vec<Expr>) {
let param = &param.name;
expanded_params.push(parse2(quote!(#param.into())).unwrap());
}
ast::TypeName::Primitive(ast::PrimitiveType::char) => {
let name = &param.name;
// TODO(#318): don't just unwrap? or should we assume that the other side gives us a good value?
expanded_params.push(parse2(quote! { char::from_u32(#name).unwrap() }).unwrap());
}
_ => {
expanded_params.push(Expr::Path(ExprPath {
attrs: vec![],
Expand Down Expand Up @@ -279,6 +266,8 @@ fn gen_bridge(input: ItemMod) -> ItemMod {
let module = ast::Module::from_syn(&input, true);
let (brace, mut new_contents) = input.content.unwrap();

new_contents.push(parse2(quote! { use diplomat_runtime::*; }).unwrap());

new_contents.iter_mut().for_each(|c| match c {
Item::Struct(s) => {
let info = AttributeInfo::extract(&mut s.attrs);
Expand Down
1 change: 1 addition & 0 deletions macro/src/snapshots/diplomat__tests__cfgd_struct.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod ffi {
unimplemented!()
}
}
use diplomat_runtime::*;
#[no_mangle]
#[cfg(feature = "foo")]
extern "C" fn Foo_bar(s: u8) {
Expand Down
1 change: 1 addition & 0 deletions macro/src/snapshots/diplomat__tests__cfged_method-2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod ffi {
unimplemented!()
}
}
use diplomat_runtime::*;
#[no_mangle]
#[cfg(feature = "foo")]
#[cfg(feature = "bar")]
Expand Down
1 change: 1 addition & 0 deletions macro/src/snapshots/diplomat__tests__cfged_method.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod ffi {
unimplemented!()
}
}
use diplomat_runtime::*;
#[no_mangle]
#[cfg(feature = "foo")]
extern "C" fn Foo_bar(s: u8) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod ffi {
unimplemented!()
}
}
use diplomat_runtime::*;
#[no_mangle]
extern "C" fn Foo_fill_slice(s_diplomat_data: *mut f64, s_diplomat_len: usize) {
Foo::fill_slice(unsafe { core::slice::from_raw_parts_mut(s_diplomat_data, s_diplomat_len) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod ffi {
unimplemented!()
}
}
use diplomat_runtime::*;
#[no_mangle]
extern "C" fn Foo_from_slice(s_diplomat_data: *const f64, s_diplomat_len: usize) {
Foo::from_slice(unsafe { core::slice::from_raw_parts(s_diplomat_data, s_diplomat_len) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod ffi {
unimplemented!()
}
}
use diplomat_runtime::*;
#[no_mangle]
extern "C" fn Foo_from_str(s_diplomat_data: *const u8, s_diplomat_len: usize) {
Foo::from_str(
Expand Down
1 change: 1 addition & 0 deletions macro/src/snapshots/diplomat__tests__mod_with_enum.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod ffi {
unimplemented!()
}
}
use diplomat_runtime::*;
#[no_mangle]
extern "C" fn Abc_do_something(this: &Abc) {
this.do_something()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod ffi {
unimplemented!()
}
}
use diplomat_runtime::*;
#[no_mangle]
extern "C" fn Foo_bar(this: &Foo) -> diplomat_runtime::DiplomatResult<(), ()> {
this.bar().into()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod ffi {
unimplemented!()
}
}
use diplomat_runtime::*;
#[no_mangle]
extern "C" fn Foo_to_string(
this: &Foo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod ffi {
Bax { foo: self }
}
}
use diplomat_runtime::*;
#[no_mangle]
extern "C" fn Bar_destroy<'b, 'a: 'b>(this: Box<Bar<'b, 'a>>) {}
#[no_mangle]
Expand Down
1 change: 1 addition & 0 deletions macro/src/snapshots/diplomat__tests__self_params.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod ffi {
unimplemented!()
}
}
use diplomat_runtime::*;
#[no_mangle]
extern "C" fn RefList_extend<'b>(this: &mut RefList<'b>, other: &RefList<'b>) -> RefList<'b> {
this.extend(other)
Expand Down
2 changes: 2 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub use writeable::DiplomatWriteable;
mod result;
pub use result::DiplomatResult;

pub type DiplomatChar = u32;

/// Allocates a buffer of a given size in Rust's memory.
///
/// # Safety
Expand Down
2 changes: 1 addition & 1 deletion tool/tests/dotnet_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ fn error_handling() {
unimplemented!()
}

pub fn bar(&self) -> Result<char, MyModuleOpaqueError> {
pub fn bar(&self) -> Result<DiplomatChar, MyModuleOpaqueError> {
unimplemented!()
}

Expand Down
Loading