Skip to content

Can't use Box<T> of the same Rust type in two different bridges #480

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

Closed
alexeyr opened this issue Nov 17, 2020 · 2 comments
Closed

Can't use Box<T> of the same Rust type in two different bridges #480

alexeyr opened this issue Nov 17, 2020 · 2 comments

Comments

@alexeyr
Copy link

alexeyr commented Nov 17, 2020

I have two extern "Rust" blocks here https://github.com/alexeyr/tokenizers/blob/f88d84b06f2503a312a051d710bd2596eba24ed4/bindings/cpp/src/normalizers.rs and here https://github.com/alexeyr/tokenizers/blob/f88d84b06f2503a312a051d710bd2596eba24ed4/bindings/cpp/src/pre_tokenizers.rs

    extern "Rust" {
        type NormalizedString;
        type NormalizerWrapper;
        type BertNormalizer;

        fn normalized_string(str: &CxxString) -> Result<Box<NormalizedString>>;

        fn bert_normalizer(
            clean_text: bool,
            handle_chinese_chars: bool,
            strip_accents: BertStripAccents,
            lowercase: bool,
        ) -> Box<BertNormalizer>;

        fn normalize_any(
            normalizer: &NormalizerWrapper,
            normalized: &mut NormalizedString,
        ) -> Result<()>;

        fn normalize_bert(
            normalizer: &BertNormalizer,
            normalized: &mut NormalizedString,
        ) -> Result<()>;
    }
    extern "Rust" {
        type NormalizedString;
        type PreTokenizedString;
        type PreTokenizerWrapper;
        type BertPreTokenizer;

        fn pre_tokenized_string(normalized: Box<NormalizedString>) -> Box<PreTokenizedString>;

        fn bert_pre_tokenizer() -> Box<BertPreTokenizer>;

        fn pre_tokenize_any(
            pre_tokenizer: &PreTokenizerWrapper,
            pre_tokenized: &mut PreTokenizedString,
        ) -> Result<()>;

        fn pre_tokenize_bert(
            pre_tokenizer: &BertPreTokenizer,
            pre_tokenized: &mut PreTokenizedString,
        ) -> Result<()>;
    }

Because both use Box<NormalizedString>, I get

error: symbol `cxxbridge05$box$huggingface$tokenizers$ffi$NormalizedString$drop` is already defined
  --> src\pre_tokenizers.rs:17:49
   |
17 |         fn pre_tokenized_string(normalized: Box<NormalizedString>) -> Box<PreTokenizedString>;
   |                                                 ^^^^^^^^^^^^^^^^

This can be avoided by using references, of course, but then I need to clone the NormalizedString (which is probably safer anyway, and what I am planning to do). Or again, by defining everything in a single module, which I'd prefer to avoid.

More generally, do I understand correctly that if I want to create an object on the Rust side and own it on the C++ side, I should use functions returning Box?

@dtolnay
Copy link
Owner

dtolnay commented Dec 1, 2020

This isn't exactly the same issue but it is very closely related to #496, so I am going to close this issue in favor of tracking it together over there. #496 (comment) outlines how I intend to resolve both limitations.

@dtolnay dtolnay closed this as completed Dec 1, 2020
@dtolnay
Copy link
Owner

dtolnay commented Dec 1, 2020

More generally, do I understand correctly that if I want to create an object on the Rust side and own it on the C++ side, I should use functions returning Box?

Correct, that's generally the best way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants