Skip to content

Use Infallible for the unconstructable default custom message type #1094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use sync::{Arc, Mutex};
use core::sync::atomic::{AtomicUsize, Ordering};
use core::{cmp, hash, fmt, mem};
use core::ops::Deref;
use core::convert::Infallible;
#[cfg(feature = "std")] use std::error;

use bitcoin::hashes::sha256::Hash as Sha256;
Expand Down Expand Up @@ -80,21 +81,21 @@ impl Deref for IgnoringMessageHandler {
fn deref(&self) -> &Self { self }
}

impl wire::Type for () {
// Implement Type for Infallible, note that it cannot be constructed, and thus you can never call a
// method that takes self for it.
impl wire::Type for Infallible {
fn type_id(&self) -> u16 {
// We should never call this for `DummyCustomType`
unreachable!();
}
}

impl Writeable for () {
impl Writeable for Infallible {
fn write<W: Writer>(&self, _: &mut W) -> Result<(), io::Error> {
unreachable!();
}
}

impl wire::CustomMessageReader for IgnoringMessageHandler {
type CustomMessage = ();
type CustomMessage = Infallible;
fn read<R: io::Read>(&self, _message_type: u16, _buffer: &mut R) -> Result<Option<Self::CustomMessage>, msgs::DecodeError> {
Ok(None)
}
Expand Down
4 changes: 4 additions & 0 deletions lightning/src/ln/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ mod tests {
}
}

impl Type for () {
fn type_id(&self) -> u16 { unreachable!(); }
}

#[test]
fn is_even_message_type() {
let message = Message::<()>::Unknown(42);
Expand Down
6 changes: 5 additions & 1 deletion lightning/src/util/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,11 @@ impl<A: Writeable, B: Writeable, C: Writeable> Writeable for (A, B, C) {
}
}

impl Writeable for () {
fn write<W: Writer>(&self, _: &mut W) -> Result<(), io::Error> {
Ok(())
}
}
impl Readable for () {
fn read<R: Read>(_r: &mut R) -> Result<Self, DecodeError> {
Ok(())
Expand All @@ -892,7 +897,6 @@ impl Writeable for String {
w.write_all(self.as_bytes())
}
}

impl Readable for String {
#[inline]
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
Expand Down