Repository navigation
Description
Hi @xrl,
I’ve realized that we don’t need to manually rewrite all functions from every class as we’re currently doing. By including the RDKit directory in the build.rs file, CXX can automatically find not just the functions in our wrappers, but also those in the RDKit library itself. This can significantly speed up development since we can directly reference the functions in the cxx::bridge.
I’ve created a small example repository to demonstrate how straightforward this is.
Highlights of the Current Wrapper
wrapper.h:
#pragma once
#include "rust/cxx.h"
#include <GraphMol/Atom.h>
#include <memory>
namespace RDKit {
std::shared_ptr<Atom> make_shared(std::unique_ptr<Atom> atom);
std::unique_ptr<Atom> newAtom();
std::unique_ptr<Atom> newAtomFromAtomicNum(int atomicNum);
std::unique_ptr<Atom> newAtomFromSymbol(const std::string &symbol);
std::unique_ptr<Atom> newAtomFromOther(const Atom &other);
rust::String getSymbolAsString(const Atom &atom);
bool MatchRust(const Atom &atom, std::unique_ptr<Atom> other);
int calcExplicitValence(Atom &atom, bool strict = true);
int calcImplicitValence(Atom &atom, bool strict = true);
} // namespace RDKitwrapper.cc:
#include "rdkit-rust-ffi/include/wrapper.h"
namespace RDKit {
std::shared_ptr<Atom> make_shared(std::unique_ptr<Atom> atom) { return std::shared_ptr<Atom>(atom.release()); }
std::unique_ptr<Atom> newAtom() { return std::unique_ptr<Atom>(new Atom()); }
std::unique_ptr<Atom> newAtomFromAtomicNum(int atomicNum) { return std::unique_ptr<Atom>(new Atom(atomicNum)); }
// Additional wrapper functions...
}Using RDKit's Built-in Functions
As seen in the repository, there are more functions listed in lib.rs than in the wrapper, but all are callable and can be used directly. For example, passing self: &Atom allows us to call functions directly like atom.getTotalValence(). This removes the need for boilerplate code like:
pub fn get_is_aromatic(&self) -> bool {
ro_mol_ffi::get_is_aromatic(self.ptr.as_ref())
}
pub fn get_atomic_num(&self) -> i32 {
ro_mol_ffi::get_atomic_num(self.ptr.as_ref())
}This simplifies usage in the rdkit crate and makes the code more maintainable.
Next Steps
We have two options:
- Refactor the existing repo to follow this approach. While it may take some time initially, it will speed up development and reduce potential errors.
- Continue building on the example repo I’ve created and start a new crate.
What are your thoughts?
Best regards,
Marco
P.S. I’ve added an explanation on how to download and compile RDKit and link the C++ library to our project. This should also add Windows support (although I haven’t tested it yet).
Activity
[-]Proposal: Simplify Rust Bindings by Leveraging RDKit's Existing Functions[/-][+]Proposal: Simplify Rust Bindings[/+]xrl commentedon Oct 10, 2024
This is very promising if it works as I'm understanding it. I am going to pull down your test repo (very nice idea!) and play around with it.
Are you interested in converting some of the existing bindings to this simplified flavor?
mvisani commentedon Oct 10, 2024
@xrl I've continued working a bit in this small repo. It actually saves a lot of time ! I'll create a pull request with a couple of modifications in the rdkit-sys crate. I'll wait for your feedback.
Add binding style evaluation for issue rdkit-rs#39
xrl commentedon Oct 9, 2026
#57 keeps direct CXX methods where they fit and uses small shims where ownership conversion, bounds checks, version differences or data conversion require them. The review covers every existing declaration and its native implementation; direct CXX syntax alone does not establish borrowing or exception safety. Keeping this design discussion open; any further simplification should preserve the checked ownership contract.
mvisani commentedon Oct 12, 2026
Hi @xrl ,
In the mean time at the Earth Metabolome Initiative, we have developed our own crates that are pure Rust and do not depend on RDKit. Here are a few examples :
We made an effort to make these crates
stdfree which allows us to compile them towasmand make them available in the browser. Feel free to check the resulting tools that we developed thanks to those crates: https://www.earthmetabolome.org/toolbox/Being RDKit free not only made huge performance improvements (parsing PUBCHEM 123 million compounds in just under a minute) but also allowed us to parse completely valid SMILES (according to OpenSMILES) that were rejected by RDKit.
I'll probably won't contribute to this repo in the future as I have a lot of work for my PhD at the moment.
By the way, we welcome contribution to our crates!
Best,
Marco
xrl commentedon Oct 19, 2026
Awesome! I will check it out. Going pure rust with the power of modern coding agents is the move. I'm not enough of a chemistry expert to feel comfortable taking it on myself. But I would love to drop a super smart, super fast, memory safe, NOT C++, library in to our structure search engine. Thanks for the note, I'll put it on my radar!