Code Monkey home page Code Monkey logo

Comments (5)

dtolnay avatar dtolnay commented on September 3, 2024 1

This would require submodule declarations within the ffi module, wouldn't it?

I meant that you would put this outside of mod ffi to organize the public Rust API. mod ffi would then be private.

from cxx.

dtolnay avatar dtolnay commented on September 3, 2024 1

some of the structs might have identical names.

I am sure you figured this out but for the benefit of anyone else reading: this means you would need to use cxx_name to deconflict them.

#[cxx::bridge]
mod bar {
    #[namespace = "foo"]
    extern "C++" {
        #[cxx_name = "Foo"]
        type FooFoo = crate::foo::Foo;
    }

    #[namespace = "bar"]
    struct Foo {
        foo: FooFoo,
    }
}

from cxx.

dtolnay avatar dtolnay commented on September 3, 2024

I am not entirely sure what you mean by different namespaces, but if it's just the C++ namespace that needs to be different, then like this:

#[cxx::bridge]
mod ffi {
    #[namespace = "foo"]
    struct Foo {
        i: i32,
    }

    #[namespace = "bar"]
    struct Bar {
        foo: Foo,
    }
}

If you additionally want them in different Rust modules, you can do:

pub mod foo {
    pub use crate::ffi::foo::Foo;
}

pub mod bar {
    pub use crate::ffi::bar::Bar;
}

or, like this:

#[cxx::bridge]
mod foo {
    #[namespace = "foo"]
    struct Foo {
        i: i32,
    }
}

// in a different file
#[cxx::bridge]
mod bar {
    #[namespace = "foo"]
    extern "C++" {
        type Foo = crate::foo::Foo;
    }

    #[namespace = "bar"]
    struct Bar {
        foo: Foo,
    }
}

from cxx.

phil-opp avatar phil-opp commented on September 3, 2024

Thanks a lot for the quick reply! I need different Rust modules too because some of the structs might have identical names.

If you additionally want them in different Rust modules, you can do:

pub mod foo {
    pub use crate::ffi::foo::Foo;
}

pub mod bar {
    pub use crate::ffi::bar::Bar;
}

This would require submodule declarations within the ffi module, wouldn't it? E.g. something like this:

#[cxx::bridge]
mod ffi {
    mod foo {
        pub struct Foo {
            foo: i32,
        }
    }

    mod bar {
        pub struct Bar {
            bar: super::foo::Foo,
        }
    }
}

Unfortunately, this does result in "unsupported item" errors for both the mod foo and mod bar declaration.


or, like this:

#[cxx::bridge]
mod foo {
    #[namespace = "foo"]
    struct Foo {
        i: i32,
    }
}

// in a different file
#[cxx::bridge]
mod bar {
    #[namespace = "foo"]
    extern "C++" {
        type Foo = crate::foo::Foo;
    }

    #[namespace = "bar"]
    struct Bar {
        foo: Foo,
    }
}

This works, thanks a lot! I didn't know that #[namespace] attributes are supported on extern "C++" blocks too.

from cxx.

phil-opp avatar phil-opp commented on September 3, 2024

Thanks!

I ended up with with the following, maybe this is useful for someone else too:

#[cxx::bridge]
mod ffi {
    #[namespace = "namespace"]
    #[cxx_name = "Name"]
    struct namespace__Name {
        field: other_namespace__OtherName,
    }
    #[namespace = "other_namespace"]
    #[cxx_name = "OtherName"]
    struct other_namespace__OtherName {
        field: i32,
    }
}

pub mod namespace {
    pub use crate::ffi::namespace__Name as Name;
}
pub mod other_namespace {
    pub use crate::ffi::other_namespace__OtherName as OtherName;
}

So I prefix every struct name in the ffi module with its namespace name to avoid name conflicts. For the C++ API, i use the namespace and cxx_name attributes to recreate the namespace and original name. For the Rust API, I use re-exports as suggested above.

from cxx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.