From: Matt Corallo Date: Tue, 4 Jan 2022 20:30:37 +0000 (+0000) Subject: Swap std references in generated code for core/alloc references X-Git-Tag: v0.0.104.1^2~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=a032db3b20a0b3f70592eba1598af10b96f37b88;p=ldk-c-bindings Swap std references in generated code for core/alloc references --- diff --git a/c-bindings-gen/src/blocks.rs b/c-bindings-gen/src/blocks.rs index b2f952b..5034b09 100644 --- a/c-bindings-gen/src/blocks.rs +++ b/c-bindings-gen/src/blocks.rs @@ -61,7 +61,7 @@ pub fn write_result_block(w: &mut W, mangled_container: &str, writeln!(w, "\tpub result: *mut {},", ok_type).unwrap(); } else { writeln!(w, "\t/// Note that this value is always NULL, as there are no contents in the OK variant").unwrap(); - writeln!(w, "\tpub result: *mut std::ffi::c_void,").unwrap(); + writeln!(w, "\tpub result: *mut core::ffi::c_void,").unwrap(); } if err_type != "()" { writeln!(w, "\t/// A pointer to the contents in the error state.").unwrap(); @@ -69,7 +69,7 @@ pub fn write_result_block(w: &mut W, mangled_container: &str, writeln!(w, "\tpub err: *mut {},", err_type).unwrap(); } else { writeln!(w, "\t/// Note that this value is always NULL, as there are no contents in the Err variant").unwrap(); - writeln!(w, "\tpub err: *mut std::ffi::c_void,").unwrap(); + writeln!(w, "\tpub err: *mut core::ffi::c_void,").unwrap(); } writeln!(w, "}}").unwrap(); writeln!(w, "#[repr(C)]").unwrap(); @@ -97,7 +97,7 @@ pub fn write_result_block(w: &mut W, mangled_container: &str, if ok_type != "()" { writeln!(w, "\t\t\tresult: Box::into_raw(Box::new(o)),").unwrap(); } else { - writeln!(w, "\t\t\tresult: std::ptr::null_mut(),").unwrap(); + writeln!(w, "\t\t\tresult: core::ptr::null_mut(),").unwrap(); } writeln!(w, "\t\t}},").unwrap(); writeln!(w, "\t\tresult_ok: true,").unwrap(); @@ -117,7 +117,7 @@ pub fn write_result_block(w: &mut W, mangled_container: &str, if err_type != "()" { writeln!(w, "\t\t\terr: Box::into_raw(Box::new(e)),").unwrap(); } else { - writeln!(w, "\t\t\terr: std::ptr::null_mut(),").unwrap(); + writeln!(w, "\t\t\terr: core::ptr::null_mut(),").unwrap(); } writeln!(w, "\t\t}},").unwrap(); writeln!(w, "\t\tresult_ok: false,").unwrap(); @@ -156,22 +156,22 @@ pub fn write_result_block(w: &mut W, mangled_container: &str, writeln!(w, "\t\tlet contents = if o.result_ok {{").unwrap(); if ok_type != "()" { writeln!(w, "\t\t\tlet result = unsafe {{ o.contents.result }};").unwrap(); - writeln!(w, "\t\t\tunsafe {{ o.contents.result = std::ptr::null_mut() }};").unwrap(); + writeln!(w, "\t\t\tunsafe {{ o.contents.result = core::ptr::null_mut() }};").unwrap(); writeln!(w, "\t\t\t{}Ptr {{ result }}", mangled_container).unwrap(); } else { writeln!(w, "\t\t\tlet _ = unsafe {{ Box::from_raw(o.contents.result) }};").unwrap(); - writeln!(w, "\t\t\to.contents.result = std::ptr::null_mut();").unwrap(); - writeln!(w, "\t\t\t{}Ptr {{ result: std::ptr::null_mut() }}", mangled_container).unwrap(); + writeln!(w, "\t\t\to.contents.result = core::ptr::null_mut();").unwrap(); + writeln!(w, "\t\t\t{}Ptr {{ result: core::ptr::null_mut() }}", mangled_container).unwrap(); } writeln!(w, "\t\t}} else {{").unwrap(); if err_type != "()" { writeln!(w, "\t\t\tlet err = unsafe {{ o.contents.err }};").unwrap(); - writeln!(w, "\t\t\tunsafe {{ o.contents.err = std::ptr::null_mut(); }}").unwrap(); + writeln!(w, "\t\t\tunsafe {{ o.contents.err = core::ptr::null_mut(); }}").unwrap(); writeln!(w, "\t\t\t{}Ptr {{ err }}", mangled_container).unwrap(); } else { writeln!(w, "\t\t\tlet _ = unsafe {{ Box::from_raw(o.contents.err) }};").unwrap(); - writeln!(w, "\t\t\to.contents.err = std::ptr::null_mut();").unwrap(); - writeln!(w, "\t\t\t{}Ptr {{ err: std::ptr::null_mut() }}", mangled_container).unwrap(); + writeln!(w, "\t\t\to.contents.err = core::ptr::null_mut();").unwrap(); + writeln!(w, "\t\t\t{}Ptr {{ err: core::ptr::null_mut() }}", mangled_container).unwrap(); } writeln!(w, "\t\t}};").unwrap(); writeln!(w, "\t\tSelf {{").unwrap(); @@ -189,7 +189,7 @@ pub fn write_result_block(w: &mut W, mangled_container: &str, if ok_type != "()" { writeln!(w, "\t\t\t\tresult: Box::into_raw(Box::new(<{}>::clone(unsafe {{ &*self.contents.result }})))", ok_type).unwrap(); } else { - writeln!(w, "\t\t\t\tresult: std::ptr::null_mut()").unwrap(); + writeln!(w, "\t\t\t\tresult: core::ptr::null_mut()").unwrap(); } writeln!(w, "\t\t\t}} }}").unwrap(); writeln!(w, "\t\t}} else {{").unwrap(); @@ -197,7 +197,7 @@ pub fn write_result_block(w: &mut W, mangled_container: &str, if err_type != "()" { writeln!(w, "\t\t\t\terr: Box::into_raw(Box::new(<{}>::clone(unsafe {{ &*self.contents.err }})))", err_type).unwrap(); } else { - writeln!(w, "\t\t\t\terr: std::ptr::null_mut()").unwrap(); + writeln!(w, "\t\t\t\terr: core::ptr::null_mut()").unwrap(); } writeln!(w, "\t\t\t}} }}").unwrap(); writeln!(w, "\t\t}}").unwrap(); @@ -226,13 +226,13 @@ pub fn write_vec_block(w: &mut W, mangled_container: &str, in writeln!(w, "impl {} {{", mangled_container).unwrap(); writeln!(w, "\t#[allow(unused)] pub(crate) fn into_rust(&mut self) -> Vec<{}> {{", inner_type).unwrap(); writeln!(w, "\t\tif self.datalen == 0 {{ return Vec::new(); }}").unwrap(); - writeln!(w, "\t\tlet ret = unsafe {{ Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) }}.into();").unwrap(); - writeln!(w, "\t\tself.data = std::ptr::null_mut();").unwrap(); + writeln!(w, "\t\tlet ret = unsafe {{ Box::from_raw(core::slice::from_raw_parts_mut(self.data, self.datalen)) }}.into();").unwrap(); + writeln!(w, "\t\tself.data = core::ptr::null_mut();").unwrap(); writeln!(w, "\t\tself.datalen = 0;").unwrap(); writeln!(w, "\t\tret").unwrap(); writeln!(w, "\t}}").unwrap(); writeln!(w, "\t#[allow(unused)] pub(crate) fn as_slice(&self) -> &[{}] {{", inner_type).unwrap(); - writeln!(w, "\t\tunsafe {{ std::slice::from_raw_parts_mut(self.data, self.datalen) }}").unwrap(); + writeln!(w, "\t\tunsafe {{ core::slice::from_raw_parts_mut(self.data, self.datalen) }}").unwrap(); writeln!(w, "\t}}").unwrap(); writeln!(w, "}}").unwrap(); @@ -250,7 +250,7 @@ pub fn write_vec_block(w: &mut W, mangled_container: &str, in writeln!(w, "impl Drop for {} {{", mangled_container).unwrap(); writeln!(w, "\tfn drop(&mut self) {{").unwrap(); writeln!(w, "\t\tif self.datalen == 0 {{ return; }}").unwrap(); - writeln!(w, "\t\tunsafe {{ Box::from_raw(std::slice::from_raw_parts_mut(self.data, self.datalen)) }};").unwrap(); + writeln!(w, "\t\tunsafe {{ Box::from_raw(core::slice::from_raw_parts_mut(self.data, self.datalen)) }};").unwrap(); writeln!(w, "\t}}").unwrap(); writeln!(w, "}}").unwrap(); if clonable { @@ -258,7 +258,7 @@ pub fn write_vec_block(w: &mut W, mangled_container: &str, in writeln!(w, "\tfn clone(&self) -> Self {{").unwrap(); writeln!(w, "\t\tlet mut res = Vec::new();").unwrap(); writeln!(w, "\t\tif self.datalen == 0 {{ return Self::from(res); }}").unwrap(); - writeln!(w, "\t\tres.extend_from_slice(unsafe {{ std::slice::from_raw_parts_mut(self.data, self.datalen) }});").unwrap(); + writeln!(w, "\t\tres.extend_from_slice(unsafe {{ core::slice::from_raw_parts_mut(self.data, self.datalen) }});").unwrap(); writeln!(w, "\t\tSelf::from(res)").unwrap(); writeln!(w, "\t}}").unwrap(); writeln!(w, "}}").unwrap(); diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index f6e60cb..da1ca4f 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -530,13 +530,13 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty // Implement supertraits for the C-mapped struct. walk_supertraits!(t, Some(&types), ( ("std::cmp::Eq", _)|("core::cmp::Eq", _) => { - writeln!(w, "impl std::cmp::Eq for {} {{}}", trait_name).unwrap(); - writeln!(w, "impl std::cmp::PartialEq for {} {{", trait_name).unwrap(); + writeln!(w, "impl core::cmp::Eq for {} {{}}", trait_name).unwrap(); + writeln!(w, "impl core::cmp::PartialEq for {} {{", trait_name).unwrap(); writeln!(w, "\tfn eq(&self, o: &Self) -> bool {{ (self.eq)(self.this_arg, o) }}\n}}").unwrap(); }, ("std::hash::Hash", _)|("core::hash::Hash", _) => { - writeln!(w, "impl std::hash::Hash for {} {{", trait_name).unwrap(); - writeln!(w, "\tfn hash(&self, hasher: &mut H) {{ hasher.write_u64((self.hash)(self.this_arg)) }}\n}}").unwrap(); + writeln!(w, "impl core::hash::Hash for {} {{", trait_name).unwrap(); + writeln!(w, "\tfn hash(&self, hasher: &mut H) {{ hasher.write_u64((self.hash)(self.this_arg)) }}\n}}").unwrap(); }, ("Send", _) => {}, ("Sync", _) => {}, ("Clone", _) => { @@ -582,7 +582,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty writeln!(w, "}}\n").unwrap(); writeln!(w, "// We're essentially a pointer already, or at least a set of pointers, so allow us to be used").unwrap(); writeln!(w, "// directly as a Deref trait in higher-level structs:").unwrap(); - writeln!(w, "impl std::ops::Deref for {} {{\n\ttype Target = Self;", trait_name).unwrap(); + writeln!(w, "impl core::ops::Deref for {} {{\n\ttype Target = Self;", trait_name).unwrap(); writeln!(w, "\tfn deref(&self) -> &Self {{\n\t\tself\n\t}}\n}}").unwrap(); } @@ -642,7 +642,7 @@ fn writeln_opaque(w: &mut W, ident: &syn::Ident, struct_name: writeln!(w, "\tpub(crate) fn take_inner(mut self) -> *mut native{} {{", struct_name).unwrap(); writeln!(w, "\t\tassert!(self.is_owned);").unwrap(); writeln!(w, "\t\tlet ret = ObjOps::untweak_ptr(self.inner);").unwrap(); - writeln!(w, "\t\tself.inner = std::ptr::null_mut();").unwrap(); + writeln!(w, "\t\tself.inner = core::ptr::null_mut();").unwrap(); writeln!(w, "\t\tret").unwrap(); writeln!(w, "\t}}\n}}").unwrap(); @@ -923,7 +923,7 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ writeln!(w, "\t\tlet mut rust_obj = {} {{ inner: ObjOps::heap_alloc(obj), is_owned: true }};", ident).unwrap(); writeln!(w, "\t\tlet mut ret = {}_as_{}(&rust_obj);", ident, trait_obj.ident).unwrap(); writeln!(w, "\t\t// We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn").unwrap(); - writeln!(w, "\t\trust_obj.inner = std::ptr::null_mut();").unwrap(); + writeln!(w, "\t\trust_obj.inner = core::ptr::null_mut();").unwrap(); writeln!(w, "\t\tret.free = Some({}_free_void);", ident).unwrap(); writeln!(w, "\t\tret").unwrap(); } @@ -1186,21 +1186,21 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ let ref_type: syn::Type = syn::parse_quote!(&#path); assert!(!types.write_to_c_conversion_new_var(w, &format_ident!("a"), &*i.self_ty, Some(&gen_types), false), "We don't support new var conversions when comparing equality"); - writeln!(w, "\t// Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core").unwrap(); + writeln!(w, "\t// Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core").unwrap(); writeln!(w, "\t#[allow(deprecated)]").unwrap(); writeln!(w, "\tlet mut hasher = core::hash::SipHasher::new();").unwrap(); - write!(w, "\tstd::hash::Hash::hash(").unwrap(); + write!(w, "\tcore::hash::Hash::hash(").unwrap(); types.write_from_c_conversion_prefix(w, &ref_type, Some(&gen_types)); write!(w, "o").unwrap(); types.write_from_c_conversion_suffix(w, &ref_type, Some(&gen_types)); writeln!(w, ", &mut hasher);").unwrap(); - writeln!(w, "\tstd::hash::Hasher::finish(&hasher)\n}}").unwrap(); + writeln!(w, "\tcore::hash::Hasher::finish(&hasher)\n}}").unwrap(); } else if (path_matches_nongeneric(&trait_path.1, &["core", "clone", "Clone"]) || path_matches_nongeneric(&trait_path.1, &["Clone"])) && types.c_type_has_inner_from_path(&resolved_path) { writeln!(w, "impl Clone for {} {{", ident).unwrap(); writeln!(w, "\tfn clone(&self) -> Self {{").unwrap(); writeln!(w, "\t\tSelf {{").unwrap(); - writeln!(w, "\t\t\tinner: if <*mut native{}>::is_null(self.inner) {{ std::ptr::null_mut() }} else {{", ident).unwrap(); + writeln!(w, "\t\t\tinner: if <*mut native{}>::is_null(self.inner) {{ core::ptr::null_mut() }} else {{", ident).unwrap(); writeln!(w, "\t\t\t\tObjOps::heap_alloc(unsafe {{ &*ObjOps::untweak_ptr(self.inner) }}.clone()) }},").unwrap(); writeln!(w, "\t\t\tis_owned: true,").unwrap(); writeln!(w, "\t\t}}\n\t}}\n}}").unwrap(); diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index c3f5ad0..57b0607 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -966,7 +966,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "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::Duration"|"core::time::Duration" => Some("core::time::Duration::from_secs("), "std::time::SystemTime" => Some("(::std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs("), "bech32::u5" => Some(""), @@ -1365,12 +1365,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { let is_inner_ref = if let Some(syn::Type::Reference(_)) = single_contained { true } else { false }; if is_ref { return Some(("if ", vec![ - (".is_none() { std::ptr::null() } else { ObjOps::nonnull_ptr_to_inner(".to_owned(), + (".is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner(".to_owned(), format!("({}{}.unwrap())", var_access, if is_inner_ref { "" } else { ".as_ref()" })) ], ") }", ContainerPrefixLocation::OutsideConv)); } else { return Some(("if ", vec![ - (".is_none() { std::ptr::null_mut() } else { ".to_owned(), format!("({}.unwrap())", var_access)) + (".is_none() { core::ptr::null_mut() } else { ".to_owned(), format!("({}.unwrap())", var_access)) ], " }", ContainerPrefixLocation::OutsideConv)); } } else if self.is_primitive(&inner_path) || self.c_type_from_path(&inner_path, false, false).is_none() { @@ -1704,7 +1704,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { syn::Type::Path(p) => { let resolved = self.resolve_path(&p.path, generics); if self.crate_types.opaques.get(&resolved).is_some() { - write!(w, "crate::{} {{ inner: std::ptr::null_mut(), is_owned: true }}", resolved).unwrap(); + write!(w, "crate::{} {{ inner: core::ptr::null_mut(), is_owned: true }}", resolved).unwrap(); } else { // Assume its a manually-mapped C type, where we can just define an null() fn write!(w, "{}::null()", self.c_type_from_path(&resolved, false, false).unwrap()).unwrap(); @@ -1780,7 +1780,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { syn::Type::Slice(_) => { // Option<[]> always implies that we want to treat len() == 0 differently from // None, so we always map an Option<[]> into a pointer. - write!(w, " == std::ptr::null_mut()").unwrap(); + write!(w, " == core::ptr::null_mut()").unwrap(); EmptyValExpectedTy::ReferenceAsPointer }, _ => unimplemented!(), diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs index e5449a5..9e928a6 100644 --- a/lightning-c-bindings/src/c_types/mod.rs +++ b/lightning-c-bindings/src/c_types/mod.rs @@ -276,7 +276,7 @@ impl Transaction { } pub(crate) fn into_bitcoin(&self) -> BitcoinTransaction { if self.datalen == 0 { panic!("0-length buffer can never represent a valid Transaction"); } - ::bitcoin::consensus::encode::deserialize(unsafe { std::slice::from_raw_parts(self.data, self.datalen) }).unwrap() + ::bitcoin::consensus::encode::deserialize(unsafe { core::slice::from_raw_parts(self.data, self.datalen) }).unwrap() } pub(crate) fn from_bitcoin(btc: &BitcoinTransaction) -> Self { let vec = ::bitcoin::consensus::encode::serialize(btc); @@ -292,7 +292,7 @@ impl Drop for Transaction { } impl Clone for Transaction { fn clone(&self) -> Self { - let sl = unsafe { std::slice::from_raw_parts(self.data, self.datalen) }; + let sl = unsafe { core::slice::from_raw_parts(self.data, self.datalen) }; let mut v = Vec::new(); v.extend_from_slice(&sl); Self::from_vec(v) @@ -369,7 +369,7 @@ impl u8slice { } pub(crate) fn to_slice(&self) -> &[u8] { if self.datalen == 0 { return &[]; } - unsafe { std::slice::from_raw_parts(self.data, self.datalen) } + unsafe { core::slice::from_raw_parts(self.data, self.datalen) } } pub(crate) fn to_reader<'a>(&'a self) -> Cursor<&'a [u8]> { let sl = self.to_slice(); @@ -467,20 +467,20 @@ impl Into for &mut &'static str { impl Str { pub(crate) fn into_str(&self) -> &'static str { if self.len == 0 { return ""; } - std::str::from_utf8(unsafe { std::slice::from_raw_parts(self.chars, self.len) }).unwrap() + core::str::from_utf8(unsafe { core::slice::from_raw_parts(self.chars, self.len) }).unwrap() } pub(crate) fn into_string(mut self) -> String { let bytes = if self.len == 0 { Vec::new() } else if self.chars_is_owned { let ret = unsafe { - Box::from_raw(std::slice::from_raw_parts_mut(unsafe { self.chars as *mut u8 }, self.len)) + Box::from_raw(core::slice::from_raw_parts_mut(unsafe { self.chars as *mut u8 }, self.len)) }.into(); self.chars_is_owned = false; ret } else { let mut ret = Vec::with_capacity(self.len); - ret.extend_from_slice(unsafe { std::slice::from_raw_parts(self.chars, self.len) }); + ret.extend_from_slice(unsafe { core::slice::from_raw_parts(self.chars, self.len) }); ret }; String::from_utf8(bytes).unwrap() @@ -562,14 +562,14 @@ pub(crate) trait TakePointer { impl TakePointer<*const T> for *const T { fn take_ptr(&mut self) -> *const T { let ret = *self; - *self = std::ptr::null(); + *self = core::ptr::null(); ret } } impl TakePointer<*mut T> for *mut T { fn take_ptr(&mut self) -> *mut T { let ret = *self; - *self = std::ptr::null_mut(); + *self = core::ptr::null_mut(); ret } } @@ -642,17 +642,17 @@ impl SmartPtr { Self { ptr: Box::into_raw(Box::new(o)) } } pub(crate) fn null() -> Self { - Self { ptr: std::ptr::null_mut() } + Self { ptr: core::ptr::null_mut() } } } impl Drop for SmartPtr { fn drop(&mut self) { - if self.ptr != std::ptr::null_mut() { + if self.ptr != core::ptr::null_mut() { unsafe { Box::from_raw(self.ptr); } } } } -impl std::ops::Deref for SmartPtr { +impl core::ops::Deref for SmartPtr { type Target = *mut T; fn deref(&self) -> &*mut T { &self.ptr