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

How to pass Vec<String> or &[String] #367

Open
happydpc opened this issue Nov 4, 2020 · 7 comments
Open

How to pass Vec<String> or &[String] #367

happydpc opened this issue Nov 4, 2020 · 7 comments
Labels
C++ C++ only related issues question

Comments

@happydpc
Copy link

happydpc commented Nov 4, 2020

I want to expose a this-like function to cpp. But id can't.

pub fn test(&self, s: &Vec<String>) {}
@Dushistov Dushistov added question C++ C++ only related issues labels Nov 5, 2020
@Dushistov
Copy link
Owner

There is no predefined type conversations for this.
Mostly because of because of it need bunch of extra code for this specific case.

One way to handle this is:

type VecOfStrings = Vec<String>;

foreign_class!(
    class VecOfStrings {
        self_type VecOfStrings;

        constructor default() -> VecOfStrings {
            Vec::<String>::default()
        }

        fn at(&self, i: usize) -> &str {
            this[i].as_str()
        }
    }
);

fn test(s: &Vec<String>) {
    unimplemented!()
}

foreign_class!(
class WorkWithStrings {
    fn test(s : &VecOfStrings);
}
);

@happydpc
Copy link
Author

Thank you for your reply. It's very useful.

@35359595
Copy link

is there a plan to implement this? quite common scenario to have this type of param...

@stephan57160
Copy link
Contributor

I was about to raise another issue for the same, and I have the same question.
Any plan to implement this ?
Maybe with some help ?

@Dushistov
Copy link
Owner

Any plan to implement this ?

No specific plans for this. I my practice I need it twice, one time I use work around mentioned here, other time I wrote rule to convert Vec<String> to QStringList. But I doubt that these two variants may be used as general solution.

Maybe with some help ?

For now for C++ there are support of Vec of "primitive type" (can be generalized to any #[repr(C)] type),
and Vec of "foreign class". The great addition would be Vec of already mapped to C++, but not foreign class and not primitive type.
But not sure that this is possible.

@stephan57160
Copy link
Contributor

But I doubt that these two variants may be used as general solution.
Maybe not.
But, in case, a CppQStringList could help, like it's done for CppVariant or others.

The great addition would be Vec of already mapped to C++, but not foreign class and not primitive type.
Hmmm...
I'm pretty fresh to Rust and Flapigen, so ... not sure to understand what you have in mind...

@Dushistov
Copy link
Owner

The great addition would be Vec of already mapped to C++, but not foreign class and not primitive type.
I'm pretty fresh to Rust and Flapigen, so ... not sure to understand what you have in mind...

flapigen can works with generic rules, for now there are two generic rules for slices:

($p:r_type) <T: SwigForeignClass> &[T] => CRustObjectSlice

and

($p:r_type) <T: SwigTypeIsReprC> &[T] => CRustSlice!() {

So if somebody add the third rule, without any trait contrait
in theory it should match all types like: &[String] and &[Vec<i32>],
so it is possible to create generic rules for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++ C++ only related issues question
Projects
None yet
Development

No branches or pull requests

4 participants