[bindings] Drop the lifetime bound on `Record` for bindings builds
authorMatt Corallo <git@bluematt.me>
Wed, 20 Dec 2023 05:17:43 +0000 (05:17 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 24 Jan 2024 23:17:43 +0000 (23:17 +0000)
Because log `Record`s are now being passed by ownership to `log`,
the bindings get quite annoyed that there's a lifetime hanging
around. We already don't use this lifetime in bindings (the
`FormatArgs` is converted to a string and stored on the heap), so
we can just drop the lifetime, even though it requires some
macro'ing of the struct definition to do so.

lightning/src/util/logger.rs

index 92ea8ffed55a91c218c4481e01a83f924536585d..d1c768dc7fe19ead4480d9743fef8a7d8fbd4fd2 100644 (file)
@@ -91,10 +91,12 @@ impl Level {
        }
 }
 
+macro_rules! impl_record {
+       ($($args: lifetime)?, $($nonstruct_args: lifetime)?) => {
 /// A Record, unit of logging output with Metadata to enable filtering
 /// Module_path, file, line to inform on log's source
 #[derive(Clone, Debug)]
-pub struct Record<'a> {
+pub struct Record<$($args)?> {
        /// The verbosity level of the message.
        pub level: Level,
        /// The node id of the peer pertaining to the logged record.
@@ -118,22 +120,17 @@ pub struct Record<'a> {
        pub file: &'static str,
        /// The line containing the message.
        pub line: u32,
-
-       #[cfg(c_bindings)]
-       /// We don't actually use the lifetime parameter in C bindings (as there is no good way to
-       /// communicate a lifetime to a C, or worse, Java user).
-       _phantom: core::marker::PhantomData<&'a ()>,
 }
 
-impl<'a> Record<'a> {
+impl<$($args)?> Record<$($args)?> {
        /// Returns a new Record.
        ///
        /// This is not exported to bindings users as fmt can't be used in C
        #[inline]
-       pub fn new(
+       pub fn new<$($nonstruct_args)?>(
                level: Level, peer_id: Option<PublicKey>, channel_id: Option<ChannelId>,
                args: fmt::Arguments<'a>, module_path: &'static str, file: &'static str, line: u32
-       ) -> Record<'a> {
+       ) -> Record<$($args)?> {
                Record {
                        level,
                        peer_id,
@@ -145,11 +142,14 @@ impl<'a> Record<'a> {
                        module_path,
                        file,
                        line,
-                       #[cfg(c_bindings)]
-                       _phantom: core::marker::PhantomData,
                }
        }
 }
+} }
+#[cfg(not(c_bindings))]
+impl_record!('a, );
+#[cfg(c_bindings)]
+impl_record!(, 'a);
 
 /// A trait encapsulating the operations required of a logger.
 pub trait Logger {