Merge pull request #46 from TheBlueMatt/main
[ldk-c-bindings] / lightning-c-bindings / src / lightning / routing / mod.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
8
9 //! Structs and impls for receiving messages about the network and storing the topology live here.
10
11 use std::str::FromStr;
12 use std::ffi::c_void;
13 use core::convert::Infallible;
14 use bitcoin::hashes::Hash;
15 use crate::c_types::*;
16
17 pub mod network_graph;
18 pub mod router;
19 pub mod scorer;
20 /// An interface used to score payment channels for path finding.
21 ///
22 ///\tScoring is in terms of fees willing to be paid in order to avoid routing through a channel.
23 #[repr(C)]
24 pub struct Score {
25         /// An opaque pointer which is passed to your function implementations as an argument.
26         /// This has no meaning in the LDK, and can be NULL or any other value.
27         pub this_arg: *mut c_void,
28         /// Returns the fee in msats willing to be paid to avoid routing through the given channel.
29         #[must_use]
30         pub channel_penalty_msat: extern "C" fn (this_arg: *const c_void, short_channel_id: u64) -> u64,
31         /// Frees any resources associated with this object given its this_arg pointer.
32         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
33         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
34 }
35 unsafe impl Send for Score {}
36 unsafe impl Sync for Score {}
37 #[no_mangle]
38 pub(crate) extern "C" fn Score_clone_fields(orig: &Score) -> Score {
39         Score {
40                 this_arg: orig.this_arg,
41                 channel_penalty_msat: Clone::clone(&orig.channel_penalty_msat),
42                 free: Clone::clone(&orig.free),
43         }
44 }
45
46 use lightning::routing::Score as rustScore;
47 impl rustScore for Score {
48         fn channel_penalty_msat(&self, mut short_channel_id: u64) -> u64 {
49                 let mut ret = (self.channel_penalty_msat)(self.this_arg, short_channel_id);
50                 ret
51         }
52 }
53
54 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
55 // directly as a Deref trait in higher-level structs:
56 impl std::ops::Deref for Score {
57         type Target = Self;
58         fn deref(&self) -> &Self {
59                 self
60         }
61 }
62 /// Calls the free function if one is set
63 #[no_mangle]
64 pub extern "C" fn Score_free(this_ptr: Score) { }
65 impl Drop for Score {
66         fn drop(&mut self) {
67                 if let Some(f) = self.free {
68                         f(self.this_arg);
69                 }
70         }
71 }