]> git.bitcoin.ninja Git - ldk-java/blob - c_sharp/src/org/ldk/structs/Sleeper.cs
Update CI references to LDK 0.0.124 drop stale memchr pins
[ldk-java] / c_sharp / src / org / ldk / structs / Sleeper.cs
1 using org.ldk.impl;
2 using org.ldk.enums;
3 using org.ldk.util;
4 using System;
5
6 namespace org { namespace ldk { namespace structs {
7
8
9 /**
10  * A struct which can be used to select across many [`Future`]s at once without relying on a full
11  * async context.
12  */
13 public class Sleeper : CommonBase {
14         internal Sleeper(object _dummy, long ptr) : base(ptr) { }
15         ~Sleeper() {
16                 if (ptr != 0) { bindings.Sleeper_free(ptr); }
17         }
18
19         /**
20          * Constructs a new sleeper from one future, allowing blocking on it.
21          */
22         public static Sleeper from_single_future(org.ldk.structs.Future future) {
23                 long ret = bindings.Sleeper_from_single_future(future.ptr);
24                 GC.KeepAlive(future);
25                 if (ret >= 0 && ret <= 4096) { return null; }
26                 org.ldk.structs.Sleeper ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.Sleeper(null, ret); }
27                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
28                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(future); };
29                 return ret_hu_conv;
30         }
31
32         /**
33          * Constructs a new sleeper from two futures, allowing blocking on both at once.
34          */
35         public static Sleeper from_two_futures(org.ldk.structs.Future fut_a, org.ldk.structs.Future fut_b) {
36                 long ret = bindings.Sleeper_from_two_futures(fut_a.ptr, fut_b.ptr);
37                 GC.KeepAlive(fut_a);
38                 GC.KeepAlive(fut_b);
39                 if (ret >= 0 && ret <= 4096) { return null; }
40                 org.ldk.structs.Sleeper ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.Sleeper(null, ret); }
41                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
42                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(fut_a); };
43                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(fut_b); };
44                 return ret_hu_conv;
45         }
46
47         /**
48          * Constructs a new sleeper from three futures, allowing blocking on all three at once.
49          */
50         public static Sleeper from_three_futures(org.ldk.structs.Future fut_a, org.ldk.structs.Future fut_b, org.ldk.structs.Future fut_c) {
51                 long ret = bindings.Sleeper_from_three_futures(fut_a.ptr, fut_b.ptr, fut_c.ptr);
52                 GC.KeepAlive(fut_a);
53                 GC.KeepAlive(fut_b);
54                 GC.KeepAlive(fut_c);
55                 if (ret >= 0 && ret <= 4096) { return null; }
56                 org.ldk.structs.Sleeper ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.Sleeper(null, ret); }
57                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
58                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(fut_a); };
59                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(fut_b); };
60                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(fut_c); };
61                 return ret_hu_conv;
62         }
63
64         /**
65          * Constructs a new sleeper on many futures, allowing blocking on all at once.
66          */
67         public static Sleeper of(Future[] futures) {
68                 long ret = bindings.Sleeper_new(InternalUtils.encodeUint64Array(InternalUtils.mapArray(futures, futures_conv_8 => futures_conv_8.ptr)));
69                 GC.KeepAlive(futures);
70                 if (ret >= 0 && ret <= 4096) { return null; }
71                 org.ldk.structs.Sleeper ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.Sleeper(null, ret); }
72                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
73                 foreach (Future futures_conv_8 in futures) { if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(futures_conv_8); };
74                 // Due to rust's strict-ownership memory model, in some cases we need to "move"
75                 // an object to pass exclusive ownership to the function being called.
76                 // In most cases, we avoid ret_hu_conv being visible in GC'd languages by cloning the object
77                 // at the FFI layer, creating a new object which Rust can claim ownership of
78                 // However, in some cases (eg here), there is no way to clone an object, and thus
79                 // we actually have to pass full ownership to Rust.
80                 // Thus, after ret_hu_conv call, futures_conv_8 is reset to null and is now a dummy object.
81                 futures_conv_8.ptr = 0;; };
82                 return ret_hu_conv;
83         }
84
85         /**
86          * Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed.
87          */
88         public void wait() {
89                 bindings.Sleeper_wait(this.ptr);
90                 GC.KeepAlive(this);
91         }
92
93         /**
94          * Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed or the
95          * given amount of time has elapsed. Returns true if a [`Future`] completed, false if the time
96          * elapsed.
97          */
98         public bool wait_timeout(long max_wait) {
99                 bool ret = bindings.Sleeper_wait_timeout(this.ptr, max_wait);
100                 GC.KeepAlive(this);
101                 GC.KeepAlive(max_wait);
102                 return ret;
103         }
104
105 }
106 } } }