Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions c-bindings-gen/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,44 @@ pub fn write_tuple_block<W: std::io::Write>(w: &mut W, mangled_container: &str,
writeln!(w, "pub extern \"C\" fn {}_free(_res: {}) {{ }}", mangled_container, mangled_container).unwrap();
}

/// Writes out a C-callable concrete Option<A> struct and utility methods
pub fn write_option_block<W: std::io::Write>(w: &mut W, mangled_container: &str, inner_type: &str, clonable: bool) {
writeln!(w, "#[repr(C)]").unwrap();
if clonable {
writeln!(w, "#[derive(Clone)]").unwrap();
}
writeln!(w, "pub enum {} {{", mangled_container).unwrap();
writeln!(w, "\tSome({}),", inner_type).unwrap();
writeln!(w, "\tNone").unwrap();
writeln!(w, "}}").unwrap();

writeln!(w, "impl {} {{", mangled_container).unwrap();
writeln!(w, "\t#[allow(unused)] pub(crate) fn is_some(&self) -> bool {{").unwrap();
writeln!(w, "\t\tif let Self::Some(_) = self {{ true }} else {{ false }}").unwrap();
writeln!(w, "\t}}").unwrap();
writeln!(w, "\t#[allow(unused)] pub(crate) fn take(mut self) -> {} {{", inner_type).unwrap();
writeln!(w, "\t\tif let Self::Some(v) = self {{ v }} else {{ unreachable!() }}").unwrap();
writeln!(w, "\t}}").unwrap();
writeln!(w, "}}").unwrap();

writeln!(w, "#[no_mangle]").unwrap();
writeln!(w, "pub extern \"C\" fn {}_some(o: {}) -> {} {{", mangled_container, inner_type, mangled_container).unwrap();
writeln!(w, "\t{}::Some(o)", mangled_container).unwrap();
writeln!(w, "}}").unwrap();

writeln!(w, "#[no_mangle]").unwrap();
writeln!(w, "pub extern \"C\" fn {}_none() -> {} {{", mangled_container, mangled_container).unwrap();
writeln!(w, "\t{}::None", mangled_container).unwrap();
writeln!(w, "}}").unwrap();

writeln!(w, "#[no_mangle]").unwrap();
writeln!(w, "pub extern \"C\" fn {}_free(_res: {}) {{ }}", mangled_container, mangled_container).unwrap();
if clonable {
writeln!(w, "#[no_mangle]").unwrap();
writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{ orig.clone() }}", mangled_container, mangled_container, mangled_container).unwrap();
}
}

/// Prints the docs from a given attribute list unless its tagged no export
pub fn writeln_docs<W: std::io::Write>(w: &mut W, attrs: &[syn::Attribute], prefix: &str) {
for attr in attrs.iter() {
Expand Down
245 changes: 177 additions & 68 deletions c-bindings-gen/src/types.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lightning-c-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ crate-type = ["staticlib"
bitcoin = "0.26"
secp256k1 = { version = "0.20.1", features = ["global-context-less-secure"] }
# Note that the following line is matched by genbindings to update the path
lightning = { git = "https://git.bitcoin.ninja/rust-lightning", rev = "8a8c75a8fc96e5c8ed59e6d80a517bc59215b4d6" }
lightning = { git = "https://git.bitcoin.ninja/rust-lightning", rev = "6fcac8bc65ed6d372e0b8c367e9934c754f99ff3" }

[patch.crates-io]
# Rust-Secp256k1 PR 279. Should be dropped once merged.
Expand Down
Loading