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

Exposing shared constants #1051

Open
str4d opened this issue May 26, 2022 · 2 comments
Open

Exposing shared constants #1051

str4d opened this issue May 26, 2022 · 2 comments

Comments

@str4d
Copy link

str4d commented May 26, 2022

I'd like to be able to expose Rust constants (for the purpose of this issue, assume only const primitives) to C++, so they are defined in a single location. For example, I'd like to write something like:

#[cxx::bridge(namespace = "blake2b")]
mod ffi {
    const PERSONALBYTES: usize = blake2b_simd::PERSONALBYTES;
}

or

#[cxx::bridge(namespace = "blake2b")]
mod ffi {
    const PERSONALBYTES;
}

const PERSONALBYTES: usize = blake2b_simd::PERSONALBYTES;

and have this be accessible from C++ as blake2b::PERSONALBYTES with type size_t.

@shymega
Copy link

shymega commented Mar 26, 2023

This is something I'd like to achieve as well. I looked at your PR there on zcash/zcash#5971, @str4d - did you manage to find a workaround?

@dtolnay
Copy link
Owner

dtolnay commented Mar 26, 2023

Probably this:

// .rs

#[cxx::bridge(namespace = "blake2b")]
mod ffi {
    extern "Rust" {
        fn get_personalbytes() -> usize;
    }
}

fn get_personalbytes() -> usize { blake2b_simd::PERSONALBYTES }
// .h

#pragma once
#include <cstddef>

namespace blake2b {
std::size_t const extern PERSONALBYTES;
}
// .cpp

#include "path/to/this.h"
#include "path/to/this.rs.h"

std::size_t const blake2b::PERSONALBYTES = get_personalbytes();

or this:

// .rs

#[no_mangle]
#[export_name = "blake2b_simd$PERSONALBYTES"]
pub static blake2b_simd_PERSONALBYTES: usize = blake2b_simd::PERSONALBYTES;
// .h

#pragma once
#include <cstddef>

std::size_t const extern blake2b_simd$PERSONALBYTES;

namespace blake2b {
std::size_t const static PERSONALBYTES = blake2b_simd$PERSONALBYTES;
}

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

3 participants