rust-lang / rust-lang/rust-bindgen

Template specialization

Open
#24 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-C++ A-templates enhancement
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Hi,
bindgen ignore template specialization for class (and struct) and select the first "working answer".
For example :

template <typename T>
class test;

template<>
class test<int> {
    int foo;
};
template<>
class test<float> {
    float foo;
};

give

test.hpp:1:9: warning: #pragma once in main file [-Wpragma-once-outside-header]
/* automatically generated by rust-bindgen */

#[repr(C)]
#[derive(Debug, Copy)]
pub struct Struct_test {
    pub foo: ::std::os::raw::c_int,
}
impl ::std::clone::Clone for Struct_test {
    fn clone(&self) -> Self { *self }
}
#[test]
fn bindgen_test_layout_Struct_test() {
    assert_eq!(::std::mem::size_of::<Struct_test>() , 4usize);
    assert_eq!(::std::mem::align_of::<Struct_test>() , 4usize);
}

The float specialization is ignored.
And with a valid test<T>:

template <typename T>
class test{};

template<>
class test<int> {
    int foo;
};
template<>
class test<float> {
    float foo;
};

gives :

test.hpp:1:9: warning: #pragma once in main file [-Wpragma-once-outside-header]
/* automatically generated by rust-bindgen */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Struct_test<T> {
    pub _phantom0: ::std::marker::PhantomData<T>,
}

Both the float and int specialization are ignored.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue using the two C++ examples and compare the generated Rust output with the reported results. Trace how bindgen handles class template declarations and explicit specializations; done means distinct int and float specializations are represented rather than silently discarded, with coverage for both examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.