Drop use of RefCell in DebugIter
authorWilmer Paulino <wilmer@wilmerpaulino.com>
Mon, 17 Jul 2023 17:45:39 +0000 (10:45 -0700)
committerWilmer Paulino <wilmer@wilmerpaulino.com>
Mon, 17 Jul 2023 17:57:34 +0000 (10:57 -0700)
The `RefCell` was necessary in a previous iteration of the code in which
the iterator was not `Clone` so we needed interior mutability in order
to consume the iterator. Now that it is `Clone`, we can drop it, as
we're no longer mutating the original iterator.

lightning/src/util/logger.rs
lightning/src/util/macro_logger.rs

index 722a81f7ab665b15961363a1f064377718e36dd3..dbca9b785e85dfbaf3c68253e7e47b91225c6bdc 100644 (file)
@@ -173,18 +173,15 @@ impl<'a> core::fmt::Display for DebugBytes<'a> {
 ///
 /// This is not exported to bindings users as fmt can't be used in C
 #[doc(hidden)]
-pub struct DebugIter<T: fmt::Display, I: core::iter::Iterator<Item = T> + Clone>(pub core::cell::RefCell<I>);
+pub struct DebugIter<T: fmt::Display, I: core::iter::Iterator<Item = T> + Clone>(pub I);
 impl<T: fmt::Display, I: core::iter::Iterator<Item = T> + Clone> fmt::Display for DebugIter<T, I> {
        fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
-               use core::ops::DerefMut;
                write!(f, "[")?;
-               let iter_ref = self.0.clone();
-               let mut iter = iter_ref.borrow_mut();
-               for item in iter.deref_mut() {
+               let mut iter = self.0.clone();
+               if let Some(item) = iter.next() {
                        write!(f, "{}", item)?;
-                       break;
                }
-               for item in iter.deref_mut() {
+               while let Some(item) = iter.next() {
                        write!(f, ", {}", item)?;
                }
                write!(f, "]")?;
index 4703bcdb32a2a00719f4d7a5eb1edd37aa2d75ec..e79980370342ff01920fe5a37b4423719a228ac7 100644 (file)
@@ -19,7 +19,7 @@ use crate::util::logger::DebugBytes;
 
 macro_rules! log_iter {
        ($obj: expr) => {
-               $crate::util::logger::DebugIter(core::cell::RefCell::new($obj))
+               $crate::util::logger::DebugIter($obj)
        }
 }