From 2bdcba6c2297ca3cabc45c484e35c058ee7d4739 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 22 Sep 2021 23:13:34 +0000 Subject: [PATCH] Map core::convert::Infallible to a similar manually-defined struct --- c-bindings-gen/src/main.rs | 2 +- c-bindings-gen/src/types.rs | 10 ++++++++++ lightning-c-bindings/src/c_types/mod.rs | 10 ++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 1572b31..38704b2 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -33,7 +33,7 @@ mod blocks; use types::*; use blocks::*; -const DEFAULT_IMPORTS: &'static str = "\nuse std::str::FromStr;\nuse std::ffi::c_void;\nuse bitcoin::hashes::Hash;\nuse crate::c_types::*;\n"; +const DEFAULT_IMPORTS: &'static str = "\nuse std::str::FromStr;\nuse std::ffi::c_void;\nuse core::convert::Infallible;\nuse bitcoin::hashes::Hash;\nuse crate::c_types::*;\n"; // ************************************* // *** Manually-expanded conversions *** diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index b1224a9..d15cec2 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -889,6 +889,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "std::time::SystemTime" => Some("u64"), "std::io::Error" => Some("crate::c_types::IOError"), + "core::convert::Infallible" => Some("crate::c_types::NotConstructable"), + "bech32::u5" => Some("crate::c_types::u5"), "core::num::NonZeroU8" => Some("u8"), @@ -967,6 +969,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { // Note that we'll panic for String if is_ref, as we only have non-owned memory, we // cannot create a &String. + "core::convert::Infallible" => Some("panic!(\"You must never construct a NotConstructable! : "), + "std::time::Duration"|"core::time::Duration" => Some("std::time::Duration::from_secs("), "std::time::SystemTime" => Some("(::std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs("), @@ -1045,6 +1049,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "alloc::string::String"|"String" => Some(".into_string()"), "std::io::Error" if !is_ref => Some(".to_rust()"), + "core::convert::Infallible" => Some("\")"), + "std::time::Duration"|"core::time::Duration" => Some(")"), "std::time::SystemTime" => Some("))"), @@ -1136,6 +1142,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "std::time::SystemTime" => Some(""), "std::io::Error" if !is_ref => Some("crate::c_types::IOError::from_rust("), + "core::convert::Infallible" => Some("panic!(\"Cannot construct an Infallible: "), + "bech32::u5" => Some(""), "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey" @@ -1208,6 +1216,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "std::time::SystemTime" => Some(".duration_since(::std::time::SystemTime::UNIX_EPOCH).expect(\"Times must be post-1970\").as_secs()"), "std::io::Error" if !is_ref => Some(")"), + "core::convert::Infallible" => Some("\")"), + "bech32::u5" => Some(".into()"), "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey" diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs index 5db6dda..18d8a08 100644 --- a/lightning-c-bindings/src/c_types/mod.rs +++ b/lightning-c-bindings/src/c_types/mod.rs @@ -17,6 +17,16 @@ use std::convert::TryInto; // Bindings need at least rustc 1.34 use std::io::{Cursor, Read}; // TODO: We should use core2 here when we support no_std +#[repr(C)] +/// A dummy struct of which an instance must never exist. +/// This corresponds to the Rust type `Infallible`, or, in unstable rust, `!` +pub struct NotConstructable { + _priv_thing: core::convert::Infallible, +} +impl From for NotConstructable { + fn from(_: core::convert::Infallible) -> Self { unreachable!(); } +} + /// Integer in the range `0..32` #[derive(PartialEq, Eq, Copy, Clone)] #[allow(non_camel_case_types)] -- 2.30.2