Skip to content

Commit 45e88b3

Browse files
committed
Use core types in no_std builds
In no_std builds, replace std with core types where available
1 parent 452a550 commit 45e88b3

27 files changed

+120
-44
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ use utils::test_persister::TestPersister;
5656
use bitcoin::secp256k1::key::{PublicKey,SecretKey};
5757
use bitcoin::secp256k1::Secp256k1;
5858

59-
use std::mem;
60-
use std::cmp::Ordering;
59+
#[cfg(not(feature = "no_std"))]
60+
use std::{cmp::Ordering, mem, sync::atomic};
61+
#[cfg(feature = "no_std")]
62+
use core::{cmp::Ordering, mem, sync::atomic};
6163
#[cfg(not(feature = "no_std"))]
6264
use std::collections::{HashSet, hash_map, HashMap};
6365
#[cfg(feature = "no_std")]
6466
use hashbrown::{HashMap, HashSet, hash_map};
6567
use std::sync::{Arc,Mutex};
66-
use std::sync::atomic;
6768
use std::io::Cursor;
6869

6970
struct FuzzEstimator {}

fuzz/src/full_stack.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ use utils::test_persister::TestPersister;
4848
use bitcoin::secp256k1::key::{PublicKey,SecretKey};
4949
use bitcoin::secp256k1::Secp256k1;
5050

51-
use std::cell::RefCell;
51+
#[cfg(not(feature = "no_std"))]
52+
use std::{cell::RefCell, cmp, sync::atomic::{AtomicU64, AtomicUsize, Ordering}};
53+
#[cfg(feature = "no_std")]
54+
use core::{cell::RefCell, cmp, sync::atomic::{AtomicU64, AtomicUsize, Ordering}};
5255
#[cfg(not(feature = "no_std"))]
5356
use std::collections::{HashMap, hash_map};
5457
#[cfg(feature = "no_std")]
5558
use hashbrown::{HashMap, hash_map};
56-
use std::cmp;
5759
use std::sync::Arc;
58-
use std::sync::atomic::{AtomicU64,AtomicUsize,Ordering};
5960

6061
#[inline]
6162
pub fn slice_to_be16(v: &[u8]) -> u16 {

fuzz/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ pub mod router;
2121

2222
pub mod msg_targets;
2323

24+
#[cfg(feature = "no_std")] extern crate core;
2425
#[cfg(feature = "no_std")] extern crate hashbrown;

fuzz/src/router.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ use std::collections::HashSet;
3131
#[cfg(feature = "no_std")]
3232
use hashbrown::HashSet;
3333
use std::sync::Arc;
34+
#[cfg(not(feature = "no_std"))]
3435
use std::sync::atomic::{AtomicUsize, Ordering};
36+
#[cfg(feature = "no_std")]
37+
use core::sync::atomic::{AtomicUsize, Ordering};
3538

3639
#[inline]
3740
pub fn slice_to_be16(v: &[u8]) -> u16 {

lightning/src/chain/channelmonitor.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ use util::events::Event;
5555
use std::collections::{HashMap, HashSet, hash_map};
5656
#[cfg(feature = "no_std")]
5757
use hashbrown::{HashMap, HashSet, hash_map};
58-
use std::{cmp, mem};
58+
#[cfg(not(feature = "no_std"))]
59+
use std::{cmp, mem, ops::Deref};
60+
#[cfg(feature = "no_std")]
61+
use core::{cmp, mem, ops::Deref};
5962
use std::io::Error;
60-
use std::ops::Deref;
6163
use std::sync::Mutex;
6264

6365
/// An update generated by the underlying Channel itself which contains some new information the

lightning/src/chain/keysinterface.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ use ln::msgs::UnsignedChannelAnnouncement;
4040
use std::collections::HashSet;
4141
#[cfg(feature = "no_std")]
4242
use hashbrown::HashSet;
43+
#[cfg(not(feature = "no_std"))]
4344
use std::sync::atomic::{AtomicUsize, Ordering};
45+
#[cfg(feature = "no_std")]
46+
use core::sync::atomic::{AtomicUsize, Ordering};
4447
use std::io::Error;
4548
use ln::msgs::{DecodeError, MAX_VALUE_MSAT};
4649

lightning/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern crate bitcoin;
3434
#[cfg(any(test, feature = "_test_utils"))] extern crate hex;
3535
#[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))] extern crate regex;
3636

37+
#[cfg(feature = "no_std")] extern crate core;
3738
#[cfg(feature = "no_std")] extern crate hashbrown;
3839

3940
#[macro_use]

lightning/src/ln/chan_utils.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ use bitcoin::secp256k1::{Secp256k1, Signature, Message};
3131
use bitcoin::secp256k1::Error as SecpError;
3232
use bitcoin::secp256k1;
3333

34-
use std::cmp;
34+
#[cfg(not(feature = "no_std"))]
35+
use std::{cmp, ops::Deref};
36+
#[cfg(feature = "no_std")]
37+
use core::{cmp, ops::Deref};
3538
use ln::chan_utils;
3639
use util::transaction_utils::sort_outputs;
3740
use ln::channel::INITIAL_COMMITMENT_NUMBER;
3841
use std::io::Read;
39-
use std::ops::Deref;
4042
use chain;
4143

4244
const HTLC_OUTPUT_IN_COMMITMENT_SIZE: usize = 1 + 8 + 4 + 32 + 5;

lightning/src/ln/channel.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ use util::logger::Logger;
3838
use util::errors::APIError;
3939
use util::config::{UserConfig,ChannelConfig};
4040

41-
use std;
42-
use std::{cmp,mem,fmt};
43-
use std::ops::Deref;
41+
#[cfg(not(feature = "no_std"))]
42+
use std::{cmp, mem, fmt, ops::Deref};
43+
#[cfg(feature = "no_std")]
44+
use core::{cmp, mem, fmt, ops::Deref};
4445
#[cfg(any(test, feature = "fuzztarget"))]
4546
use std::sync::Mutex;
4647
use bitcoin::hashes::hex::ToHex;

lightning/src/ln/channelmanager.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,32 @@ use util::chacha20::{ChaCha20, ChaChaReader};
5555
use util::logger::Logger;
5656
use util::errors::APIError;
5757

58-
use std::{cmp, mem};
58+
#[cfg(not(feature = "no_std"))]
59+
use std::{
60+
cmp,
61+
mem,
62+
marker::{Sync, Send},
63+
ops::Deref,
64+
sync::atomic::{AtomicUsize, Ordering},
65+
time::Duration,
66+
};
67+
#[cfg(feature = "no_std")]
68+
use core::{
69+
cmp,
70+
mem,
71+
marker::{Sync, Send},
72+
ops::Deref,
73+
sync::atomic::{AtomicUsize, Ordering},
74+
time::Duration,
75+
};
5976
#[cfg(not(feature = "no_std"))]
6077
use std::collections::{HashMap, hash_map, HashSet};
6178
#[cfg(feature = "no_std")]
6279
use hashbrown::{HashMap, HashSet, hash_map};
6380
use std::io::{Cursor, Read};
6481
use std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard};
65-
use std::sync::atomic::{AtomicUsize, Ordering};
66-
use std::time::Duration;
6782
#[cfg(any(test, feature = "allow_wallclock_use"))]
6883
use std::time::Instant;
69-
use std::marker::{Sync, Send};
70-
use std::ops::Deref;
7184
use bitcoin::hashes::hex::ToHex;
7285

7386
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
@@ -4309,7 +4322,10 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
43094322
mod tests {
43104323
use ln::channelmanager::PersistenceNotifier;
43114324
use std::sync::Arc;
4325+
#[cfg(not(feature = "no_std"))]
43124326
use std::sync::atomic::{AtomicBool, Ordering};
4327+
#[cfg(feature = "no_std")]
4328+
use core::sync::atomic::{AtomicBool, Ordering};
43134329
use std::thread;
43144330
use std::time::Duration;
43154331

lightning/src/ln/features.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
//! [`Features`]: struct.Features.html
2626
//! [`Context`]: sealed/trait.Context.html
2727
28-
use std::{cmp, fmt};
29-
use std::marker::PhantomData;
28+
#[cfg(not(feature = "no_std"))]
29+
use std::{cmp, fmt, marker::PhantomData};
30+
#[cfg(feature = "no_std")]
31+
use core::{cmp, fmt, marker::PhantomData};
3032

3133
use ln::msgs::DecodeError;
3234
use util::ser::{Readable, Writeable, Writer};

lightning/src/ln/functional_test_utils.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ use bitcoin::hash_types::BlockHash;
3838

3939
use bitcoin::secp256k1::key::PublicKey;
4040

41-
use std::cell::RefCell;
4241
use std::rc::Rc;
4342
use std::sync::Mutex;
44-
use std::mem;
43+
#[cfg(not(feature = "no_std"))]
44+
use std::{cell::RefCell, mem};
45+
#[cfg(feature = "no_std")]
46+
use core::{cell::RefCell, mem};
4547
#[cfg(not(feature = "no_std"))]
4648
use std::collections::HashMap;
4749
#[cfg(feature = "no_std")]

lightning/src/ln/functional_tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ use std::collections::{HashMap, HashSet};
5454
use hashbrown::{HashMap, HashSet};
5555
use std::default::Default;
5656
use std::sync::Mutex;
57+
#[cfg(not(feature = "no_std"))]
5758
use std::sync::atomic::Ordering;
59+
#[cfg(feature = "no_std")]
60+
use core::sync::atomic::Ordering;
5861

5962
use ln::functional_test_utils::*;
6063
use ln::chan_utils::CommitmentTransaction;

lightning/src/ln/msgs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ use bitcoin::hash_types::{Txid, BlockHash};
3232

3333
use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
3434

35-
use std::{cmp, fmt};
36-
use std::fmt::Debug;
35+
#[cfg(not(feature = "no_std"))]
36+
use std::{cmp, fmt::{self, Debug}};
37+
#[cfg(feature = "no_std")]
38+
use core::{cmp, fmt::{self, Debug}};
3739
use std::io::Read;
3840

3941
use util::events::MessageSendEventsProvider;

lightning/src/ln/onchaintx.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ use util::byte_utils;
3636
use std::collections::{HashMap, hash_map};
3737
#[cfg(feature = "no_std")]
3838
use hashbrown::{HashMap, hash_map};
39-
use std::cmp;
40-
use std::ops::Deref;
41-
use std::mem::replace;
39+
#[cfg(not(feature = "no_std"))]
40+
use std::{cmp, mem::replace, ops::Deref};
41+
#[cfg(feature = "no_std")]
42+
use core::{cmp, mem::replace, ops::Deref};
4243

4344
const MAX_ALLOC_SIZE: usize = 64*1024;
4445

lightning/src/ln/onion_route_tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ use bitcoin::secp256k1::Secp256k1;
3333
use bitcoin::secp256k1::key::SecretKey;
3434

3535
use std::default::Default;
36+
#[cfg(not(feature = "no_std"))]
3637
use std::sync::atomic::Ordering;
38+
#[cfg(feature = "no_std")]
39+
use core::sync::atomic::Ordering;
3740
use std::io;
3841

3942
use ln::functional_test_utils::*;

lightning/src/ln/peer_handler.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ use std::collections::{HashMap, HashSet, hash_map};
3636
#[cfg(feature = "no_std")]
3737
use hashbrown::{HashMap, HashSet, hash_map};
3838
use std::sync::{Arc, Mutex};
39-
use std::sync::atomic::{AtomicUsize, Ordering};
40-
use std::{cmp, error, hash, fmt, mem};
41-
use std::ops::Deref;
39+
#[cfg(not(feature = "no_std"))]
40+
use std::{cmp, hash, fmt, mem, ops::Deref, sync::atomic::{AtomicUsize, Ordering}};
41+
#[cfg(feature = "no_std")]
42+
use core::{cmp, hash, fmt, mem, ops::Deref, sync::atomic::{AtomicUsize, Ordering}};
43+
use std::error;
4244

4345
use bitcoin::hashes::sha256::Hash as Sha256;
4446
use bitcoin::hashes::sha256::HashEngine as Sha256Engine;
@@ -1412,9 +1414,11 @@ mod tests {
14121414
use bitcoin::secp256k1::Secp256k1;
14131415
use bitcoin::secp256k1::key::{SecretKey, PublicKey};
14141416

1415-
use std;
14161417
use std::sync::{Arc, Mutex};
1417-
use std::sync::atomic::Ordering;
1418+
#[cfg(not(feature = "no_std"))]
1419+
use std::{hash::{Hash, Hasher}, sync::atomic::Ordering};
1420+
#[cfg(feature = "no_std")]
1421+
use core::{hash::{Hash, Hasher}, sync::atomic::Ordering};
14181422

14191423
#[derive(Clone)]
14201424
struct FileDescriptor {
@@ -1427,8 +1431,8 @@ mod tests {
14271431
}
14281432
}
14291433
impl Eq for FileDescriptor { }
1430-
impl std::hash::Hash for FileDescriptor {
1431-
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
1434+
impl Hash for FileDescriptor {
1435+
fn hash<H: Hasher>(&self, hasher: &mut H) {
14321436
self.fd.hash(hasher)
14331437
}
14341438
}

lightning/src/ln/reorg_tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ use bitcoin::hash_types::BlockHash;
2727
use std::collections::HashMap;
2828
#[cfg(feature = "no_std")]
2929
use hashbrown::HashMap;
30+
#[cfg(not(feature = "no_std"))]
3031
use std::mem;
32+
#[cfg(feature = "no_std")]
33+
use core::mem;
3134

3235
use ln::functional_test_utils::*;
3336

lightning/src/routing/network_graph.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ use util::ser::{Writeable, Readable, Writer};
3131
use util::logger::Logger;
3232
use util::events::{MessageSendEvent, MessageSendEventsProvider};
3333

34-
use std::{cmp, fmt};
34+
#[cfg(not(feature = "no_std"))]
35+
use std::{cmp, fmt, ops::Deref};
36+
#[cfg(feature = "no_std")]
37+
use core::{cmp, fmt, ops::Deref};
3538
use std::sync::{RwLock, RwLockReadGuard};
39+
#[cfg(not(feature = "no_std"))]
3640
use std::sync::atomic::{AtomicUsize, Ordering};
41+
#[cfg(feature = "no_std")]
42+
use core::sync::atomic::{AtomicUsize, Ordering};
3743
use std::sync::Mutex;
3844
use std::collections::BTreeMap;
3945
use std::collections::btree_map::Entry as BtreeEntry;
40-
use std::ops::Deref;
4146
use bitcoin::hashes::hex::ToHex;
4247

4348
/// The maximum number of extra bytes which we do not understand in a gossip message before we will

lightning/src/routing/router.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ use routing::network_graph::{NetworkGraph, RoutingFees};
2121
use util::ser::{Writeable, Readable};
2222
use util::logger::Logger;
2323

24-
use std::cmp;
24+
#[cfg(not(feature = "no_std"))]
25+
use std::{cmp, ops::Deref};
26+
#[cfg(feature = "no_std")]
27+
use core::{cmp, ops::Deref};
2528
use std::collections::BinaryHeap;
2629
#[cfg(not(feature = "no_std"))]
2730
use std::collections::HashMap;
2831
#[cfg(feature = "no_std")]
2932
use hashbrown::HashMap;
30-
use std::ops::Deref;
3133

3234
/// A hop in a route
3335
#[derive(Clone, PartialEq)]

lightning/src/util/chacha20.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use std::io;
1313

1414
#[cfg(not(feature = "fuzztarget"))]
1515
mod real_chacha {
16+
#[cfg(not(feature = "no_std"))]
1617
use std::cmp;
18+
#[cfg(feature = "no_std")]
19+
use core::cmp;
1720
use util::byte_utils::{slice_to_le32, le32_to_array};
1821

1922
#[derive(Clone, Copy, PartialEq, Eq)]

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitment
1111
use ln::{chan_utils, msgs};
1212
use chain::keysinterface::{Sign, InMemorySigner};
1313

14+
#[cfg(not(feature = "no_std"))]
1415
use std::cmp;
16+
#[cfg(feature = "no_std")]
17+
use core::cmp;
1518
use std::sync::{Mutex, Arc};
1619

1720
use bitcoin::blockdata::transaction::{Transaction, SigHashType};

lightning/src/util/logger.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
//! The second one, client-side by implementing check against Record Level field.
1515
//! Each module may have its own Logger or share one.
1616
17-
use std::cmp;
18-
use std::fmt;
17+
#[cfg(not(feature = "no_std"))]
18+
use std::{cmp, fmt};
19+
#[cfg(feature = "no_std")]
20+
use core::{cmp, fmt};
1921

2022
static LOG_LEVEL_NAMES: [&'static str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
2123

lightning/src/util/poly1305.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
// This is a port of Andrew Moons poly1305-donna
88
// https://github.com/floodyberry/poly1305-donna
99

10+
#[cfg(not(feature = "no_std"))]
1011
use std::cmp::min;
12+
#[cfg(feature = "no_std")]
13+
use core::cmp::min;
1114
use util::byte_utils::{slice_to_le32, le32_to_array};
1215

1316
#[derive(Clone, Copy)]

lightning/src/util/ser.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ use std::io::{Read, Write};
1515
use std::collections::HashMap;
1616
#[cfg(feature = "no_std")]
1717
use hashbrown::HashMap;
18-
use std::hash::Hash;
1918
use std::sync::Mutex;
20-
use std::cmp;
19+
#[cfg(not(feature = "no_std"))]
20+
use std::{cmp, hash::Hash};
21+
#[cfg(feature = "no_std")]
22+
use core::{cmp, hash::Hash};
2123

2224
use bitcoin::secp256k1::Signature;
2325
use bitcoin::secp256k1::key::{PublicKey, SecretKey};

0 commit comments

Comments
 (0)