|
28 | 28 | #![allow(bare_trait_objects)] |
29 | 29 | #![allow(ellipsis_inclusive_range_patterns)] |
30 | 30 |
|
| 31 | +#![cfg_attr(all(not(feature = "std"), not(test)), no_std)] |
| 32 | + |
31 | 33 | #![cfg_attr(all(any(test, feature = "_test_utils"), feature = "unstable"), feature(test))] |
32 | 34 | #[cfg(all(any(test, feature = "_test_utils"), feature = "unstable"))] extern crate test; |
33 | 35 |
|
| 36 | +#[cfg(not(any(feature = "std", feature = "no_std")))] |
| 37 | +compile_error!("at least one of the `std` or `no_std` features must be enabled"); |
| 38 | + |
34 | 39 | #[macro_use] |
35 | 40 | extern crate alloc; |
36 | 41 | extern crate bitcoin; |
| 42 | +#[cfg(any(test, feature = "std"))] |
37 | 43 | extern crate core; |
| 44 | + |
38 | 45 | #[cfg(any(test, feature = "_test_utils"))] extern crate hex; |
39 | 46 | #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))] extern crate regex; |
40 | 47 |
|
| 48 | +#[cfg(not(feature = "std"))] extern crate core2; |
| 49 | + |
41 | 50 | #[macro_use] |
42 | 51 | pub mod util; |
43 | 52 | pub mod chain; |
44 | 53 | pub mod ln; |
45 | 54 | pub mod routing; |
46 | 55 |
|
| 56 | +#[cfg(feature = "std")] |
| 57 | +use std::io; |
| 58 | +#[cfg(not(feature = "std"))] |
| 59 | +use core2::io; |
| 60 | + |
| 61 | +#[cfg(not(feature = "std"))] |
| 62 | +mod io_extras { |
| 63 | + use core2::io::{self, Read, Write}; |
| 64 | + |
| 65 | + /// A writer which will move data into the void. |
| 66 | + pub struct Sink { |
| 67 | + _priv: (), |
| 68 | + } |
| 69 | + |
| 70 | + /// Creates an instance of a writer which will successfully consume all data. |
| 71 | + pub const fn sink() -> Sink { |
| 72 | + Sink { _priv: () } |
| 73 | + } |
| 74 | + |
| 75 | + impl core2::io::Write for Sink { |
| 76 | + #[inline] |
| 77 | + fn write(&mut self, buf: &[u8]) -> core2::io::Result<usize> { |
| 78 | + Ok(buf.len()) |
| 79 | + } |
| 80 | + |
| 81 | + #[inline] |
| 82 | + fn flush(&mut self) -> core2::io::Result<()> { |
| 83 | + Ok(()) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64, io::Error> |
| 88 | + where |
| 89 | + R: Read, |
| 90 | + W: Write, |
| 91 | + { |
| 92 | + let mut count = 0; |
| 93 | + let mut buf = [0u8; 64]; |
| 94 | + |
| 95 | + loop { |
| 96 | + match reader.read(&mut buf) { |
| 97 | + Ok(0) => break, |
| 98 | + Ok(n) => { writer.write_all(&buf[0..n])?; count += n as u64; }, |
| 99 | + Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}, |
| 100 | + Err(e) => return Err(e.into()), |
| 101 | + }; |
| 102 | + } |
| 103 | + Ok(count) |
| 104 | + } |
| 105 | + |
| 106 | + pub fn read_to_end<D: io::Read>(mut d: D) -> Result<alloc::vec::Vec<u8>, io::Error> { |
| 107 | + let mut result = vec![]; |
| 108 | + let mut buf = [0u8; 64]; |
| 109 | + loop { |
| 110 | + match d.read(&mut buf) { |
| 111 | + Ok(0) => break, |
| 112 | + Ok(n) => result.extend_from_slice(&buf[0..n]), |
| 113 | + Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}, |
| 114 | + Err(e) => return Err(e.into()), |
| 115 | + }; |
| 116 | + } |
| 117 | + Ok(result) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +#[cfg(feature = "std")] |
| 122 | +mod io_extras { |
| 123 | + pub fn read_to_end<D: ::std::io::Read>(mut d: D) -> Result<Vec<u8>, ::std::io::Error> { |
| 124 | + let mut buf = Vec::new(); |
| 125 | + d.read_to_end(&mut buf)?; |
| 126 | + Ok(buf) |
| 127 | + } |
| 128 | + |
| 129 | + pub use std::io::{copy, sink}; |
| 130 | +} |
| 131 | + |
47 | 132 | mod prelude { |
48 | 133 | #[cfg(feature = "hashbrown")] |
49 | 134 | extern crate hashbrown; |
50 | 135 |
|
51 | | - pub use alloc::{vec, vec::Vec, string::String, collections::VecDeque}; |
| 136 | + pub use alloc::{vec, vec::Vec, string::String, collections::VecDeque, boxed::Box}; |
52 | 137 | #[cfg(not(feature = "hashbrown"))] |
53 | 138 | pub use std::collections::{HashMap, HashSet, hash_map}; |
54 | 139 | #[cfg(feature = "hashbrown")] |
55 | 140 | pub use self::hashbrown::{HashMap, HashSet, hash_map}; |
| 141 | + |
| 142 | + pub use alloc::borrow::ToOwned; |
| 143 | + pub use alloc::string::ToString; |
56 | 144 | } |
57 | 145 |
|
58 | 146 | #[cfg(feature = "std")] |
|
0 commit comments