+/// [`OnionMessage`]s buffered to be sent.
+enum OnionMessageBuffer {
+ /// Messages for a node connected as a peer.
+ ConnectedPeer(VecDeque<OnionMessage>),
+
+ /// Messages for a node that is not yet connected.
+ PendingConnection(VecDeque<OnionMessage>),
+}
+
+impl OnionMessageBuffer {
+ fn pending_messages(&self) -> &VecDeque<OnionMessage> {
+ match self {
+ OnionMessageBuffer::ConnectedPeer(pending_messages) => pending_messages,
+ OnionMessageBuffer::PendingConnection(pending_messages) => pending_messages,
+ }
+ }
+
+ fn enqueue_message(&mut self, message: OnionMessage) {
+ let pending_messages = match self {
+ OnionMessageBuffer::ConnectedPeer(pending_messages) => pending_messages,
+ OnionMessageBuffer::PendingConnection(pending_messages) => pending_messages,
+ };
+
+ pending_messages.push_back(message);
+ }
+
+ fn dequeue_message(&mut self) -> Option<OnionMessage> {
+ let pending_messages = match self {
+ OnionMessageBuffer::ConnectedPeer(pending_messages) => pending_messages,
+ OnionMessageBuffer::PendingConnection(pending_messages) => {
+ debug_assert!(false);
+ pending_messages
+ },
+ };
+
+ pending_messages.pop_front()
+ }
+
+ #[cfg(test)]
+ fn release_pending_messages(&mut self) -> VecDeque<OnionMessage> {
+ let pending_messages = match self {
+ OnionMessageBuffer::ConnectedPeer(pending_messages) => pending_messages,
+ OnionMessageBuffer::PendingConnection(pending_messages) => pending_messages,
+ };
+
+ core::mem::take(pending_messages)
+ }
+
+ fn mark_connected(&mut self) {
+ if let OnionMessageBuffer::PendingConnection(pending_messages) = self {
+ let mut new_pending_messages = VecDeque::new();
+ core::mem::swap(pending_messages, &mut new_pending_messages);
+ *self = OnionMessageBuffer::ConnectedPeer(new_pending_messages);
+ }
+ }
+}
+