dushistov / flapigen-rs Goto Github PK
View Code? Open in Web Editor NEWTool for connecting programs or libraries written in Rust with other languages
License: BSD 3-Clause "New" or "Revised" License
Tool for connecting programs or libraries written in Rust with other languages
License: BSD 3-Clause "New" or "Revised" License
I see future combining rust_swig and .net to use it in Xamarin and other .net platforms. I want to now if there is any support for that.
Need conversation of CRustString
to RustString
in generated c++ code
(I am using Java, but I think this might apply to C++ as well)
Assume we have a struct with impl:
struct A;
impl A {
fn foo<B>(b: B) {
/* no-op */
}
fn bar<B>() {
/* no-op */
}
}
We can trivially export foo
to Java if we instantiate and alias the method, but there's no way to export bar
:
foreigner_class! ( class A {
method A::foo<A>(b: A); alias fooA; //works
method A::bar<A>(); // does not work,
});
Note that the generated code looks like
...
let mut ret: () = A::foo(a_0);
...
let mut ret: () = A::bar();
This means that, even if I "instantiate" the generic in the foreigner_class!
macro invocation, I cannot export A::bar
, even for concrete known types.
To conclude: is it possible and sound to use the supplied generic parameters in the foreigner_class!
invocation in the generated code? If so, can it be done?
Thanks!
Just came across your project from SO. Good work.
Thought I would let you know that syntex is no longer maintained. https://github.com/dtolnay/syn is an alternative
I see that [i8] maps to a JavaByteArray, but [u8] slices don't seem implemented. Is there a way to implement this?
To check how architecture ready to add new langauges support,
we need something simple, like c++
basic support
Simplify usage of code like this:
//rust
foreigner_class!(class Foo {
self_type Foo;
method get_str(&self) -> &str;
method set_str(&self, _: &str);
in c++
you need convert get_str
output to std::string
and then pass std::string::c_str
result to set_str
, because of &str
interpreted as char *
on C
side.
With current versoin if you add code like this:
foreigner_class!(class Uuid {
self_type Uuid;
private constructor uuid_private_constructor() -> Uuid;
static_method Uuid::new_v4() -> Uuid;
method Uuid::eq(&self, _: &Uuid) -> bool;
method Uuid::clone(&self) -> Uuid;
foreigner_code r#"
public:
bool operator==(const Uuid &o) const
{
return this->eq(o);
}
"#;
});
you can only use Uuid == Uuid
operation, not UuidRef == UuidRef
.
rust_swig is designed to be used from cargo build scripts. Just copy an appropriate code from examples crates: general java, android/java, c++ and add rust_swig as [build-dependencies] into your Cargo.toml.
I don't really like the idea of copy-pasting 250 lines of code. Would it be interesting to, for example, have gen_for_android()
inside the rust_swig
library? I think it would be nice to have a (initially opinionated) "default" implementation that can simply be called.
Hi!
I'm really liking the library so far, rust_swig
has been working quite well.
There was one thing I was wondering about though, and that's the possibility of having the jni_c_header
types and SwigInto
/ SwigFrom
shared between multiple crates which use rust_swig
.
Right now if I want to split up bindings into multiple crates that depend on eachother, there will be duplicated code and/or incompatibilities depending on what I'm doing.
Would it be reasonable to instead have all "shared" things into a swig
or swig_types
crate that would be depended upon by projects using rust_swig
?
It would have to be a separate crate from the crate providing swig macros, but this is a convention that's done by many other crates, such as serde
for shared serialization/deserialization types, and the serde_derive
for a compiler plugin for automatically implementing those types.
If it's possible, I'd also think having jni_c_headers
as part of a separate crate would reduce the complexity of the build script, and of project setup - it would remove the need for every project using rust_swig
to declare a mod jni_c_headers
containing the exact same boilerplate.
On the other hand, I'm also very new to using rust_swig
, so there might be reasons why this isn't feasible, or not useful at all. If you have the time to elaborate, I would definitely like to hear what you think about this.
Thank you for making the project, I hope you find this useful.
error: namespace rust, class XYZ: has methods but no constructor
In this case, this is what I actually want to have happen - make it so the Rust code can create a class, but not the C++ code. Is this disabled because it's impossible, or is there some way to allow this?
Example of code:
foreigner_class!(class Foo {
self_type Foo;
constructor Foo::new(_: i32, _: &str) -> Foo;
method Foo::f(&self, _: i32, _: i32) -> i32; alias calcF;
});
foreigner_class!(class TestPassObjectsAsValue {
self_type TestPassObjectsAsValue;
constructor TestPassObjectsAsValue::default() -> TestPassObjectsAsValue;
method TestPassObjectsAsValue::f1(&self, _: &Foo);
});
Option for Android is hard, so should be setting to generate our own option.
Hi,
First of all, thanks for writing this crate. It's quite ambitious.
I was trying to get this working with a project at work, and was running into a series of issues.
First, the instructions don't specify that you need to set up a build script. I started out using the one from the C++ tests folder.
Second, there's a lot of issues I encountered with cargo hanging when I had Sublime open (running RLS, linter, ...). This might be a separate issue.
Third, even after closing Sublime and creating the build script, and reconfiguring lib.rs to lib.rs.in, I still can't get it to build as it seems to be looking for rust_str.h
error: Can not put/generate foreign lang helpers: update of c++\rust_interface\rust_str.h failed: the system cannot find the path specified. (os error 3)
I'm not sure what the proper way is to get past this error. It looks like I might be able to just copy the file over, but that seems rather inelegant and undesirable as my local copy would eventually fall out of sync with the library copy...
Thanks!
If one of user function's argument has type JNIEnv
pass JNIEnv
to it.
Example:
mod swig_foreign_types_map {
#![swig_foreigner_type = "const struct MapRect *"]
#![swig_rust_type = "*const MapRect"]
}
if in function you use *<SPACE>const MapRect
rust_swig can not find such type.
foreign_interface!(interface ToString {
self_type std::string::ToString;
toString = std::string::ToString::to_string(&self) -> String;
});
thread 'main' panicked at 'not yet implemented', C:\users\spease\.cargo\registry\src\github.com-1ecc6299db9ec823\rust_swig-0.1.3\src\cpp\mod.rs:1303:18
Am I doing something wrong, or is this legitimately not possible? Thanks.
May be special macro, like foreign_enum!
?
Check how to write code generation rules,
that works for such cases:
build.rs
-> rebuildother issues related to rebuild with cargo
+ sccache
.
I assumed the generation here worked for any supported value type, but checking out the code, it seems to be handled case by case. Is there a reason Option is not supported? As a workaround, I think I can use a better error type and have missing data be one error case.
error: Do not know conversation from such rust type 'Result<Option, String>' to foreign
suppose
trait Foo {
fn bar() where Self: Sized;
}
foreign_interface! (interface FooBar {
self_type Foo;
bar = Foo::bar();
})
This crashes at compile time:
at macroslib/src/java_jni/mod.rs:218:
thread 'main' panicked at 'attempt to subtract with overflow', /home/rsmet/.cargo/registry/src/github.com-1ecc6299db9ec823/rust_swig-0.1.5/src/java_jni/mod.rs:215:67
I gave the code a quick look, and it seems to assume no "static" (is that the Rusty term?) trait methods.
They currently don't make too much sense to bind to Java though, since we can only pass them through Box
es (instead of genericly/impl Trait
), but it might make sense in the future.
#139 for Java. I cannot seem to export methods like
method foo(&mut self, Vec<Foo>);
where Foo
has been declared before.
Handle cases like:
foreigner_class!(class Boo {
self_type Boo;
constructor Boo::new(_: i32, _: usize) -> Result<Boo, String>;
static_method Boo::default() -> Boo;
This bus was introduced by 074b948
It isn't made clear how you're supposed to get headers for the classes being generated with SWIG. Thanks.
Should be const UuidRef &
, instead of const UuidOpaque *
in generated C++ code
#[test]
fn test_foreign_interface_cpp() {
let gen_code = parse_code(
"test_foreign_interface_cpp",
r#"
foreigner_class!(class Uuid {
self_type Uuid;
private constructor uuid_private_constructor() -> Uuid;
static_method Uuid::new_v4() -> Uuid;
});
foreign_interface!(interface RepoChangedCallback {
self_type RepoChangedCallback;
on_save = RepoChangedCallback::on_save(&self, uuid: &Uuid);
on_remove = RepoChangedCallback::on_remove(&self, uuid: &Uuid);
});
"#,
&[ForeignLang::Cpp],
);
let cpp_code_pair = gen_code
.iter()
.find(|x| x.lang == ForeignLang::Cpp)
.unwrap();
println!("c/c++: {}", cpp_code_pair.foreign_code);
cpp_code_pair
.foreign_code
.contains("virtual void on_save(const UuidOpaque * a_0) = 0;");
cpp_code_pair
.foreign_code
.contains("virtual void on_remove(const UuidOpaque * a_0) = 0;");
}
I would like to implement an interface in Java that can be send to Rust in a thread safe way: I basically want to guarantee Send
on the sent object Box<MyTraitInterface + Send>
. Is this somehow possible?
Mind that it's late on my day and my brain is getting laggy. I have no idea whether it's somehow possible to do.
This should prevents misusage like:
trait Boo { fn f(&self); }
foreigner_class!(class Boo {
self_type Box<Boo>;
method Boo::f(&self);
});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.