mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 04:09:23 +00:00
no-std support for monero-serai (#311)
* Move monero-serai from std to std-shims, where possible * no-std fixes * Make the HttpRpc its own feature, thiserror only on std * Drop monero-rs's epee for a homegrown one We only need it for a single function. While I tried jeffro's, it didn't work out of the box, had three unimplemented!s, and is no where near viable for no_std. Fixes #182, though should be further tested. * no-std monero-serai * Allow base58-monero via git * cargo fmt
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
@@ -10,6 +12,13 @@ pub mod sync;
|
||||
pub mod collections;
|
||||
pub mod io;
|
||||
|
||||
pub mod vec {
|
||||
#[cfg(not(feature = "std"))]
|
||||
pub use alloc::vec::*;
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::vec::*;
|
||||
}
|
||||
|
||||
pub mod str {
|
||||
#[cfg(not(feature = "std"))]
|
||||
pub use alloc::str::*;
|
||||
@@ -17,9 +26,9 @@ pub mod str {
|
||||
pub use std::str::*;
|
||||
}
|
||||
|
||||
pub mod vec {
|
||||
pub mod string {
|
||||
#[cfg(not(feature = "std"))]
|
||||
pub use alloc::vec::*;
|
||||
pub use alloc::string::*;
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::vec::*;
|
||||
pub use std::string::*;
|
||||
}
|
||||
|
||||
@@ -28,28 +28,42 @@ pub use mutex_shim::{ShimMutex as Mutex, MutexGuard};
|
||||
pub use std::sync::OnceLock;
|
||||
#[cfg(not(feature = "std"))]
|
||||
mod oncelock_shim {
|
||||
pub struct OnceLock<T>(super::Mutex<()>, Option<T>);
|
||||
use super::Mutex;
|
||||
|
||||
pub struct OnceLock<T>(Mutex<bool>, Option<T>);
|
||||
impl<T> OnceLock<T> {
|
||||
pub const fn new() -> OnceLock<T> {
|
||||
OnceLock(Mutex::new(), None)
|
||||
OnceLock(Mutex::new(false), None)
|
||||
}
|
||||
|
||||
// These return a distinct Option in case of None so another caller using get_or_init doesn't
|
||||
// transform it from None to Some
|
||||
pub fn get(&self) -> Option<&T> {
|
||||
self.1.as_ref()
|
||||
if !*self.0.lock() {
|
||||
None
|
||||
} else {
|
||||
self.1.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self) -> Option<&mut T> {
|
||||
self.1.as_mut()
|
||||
if !*self.0.lock() {
|
||||
None
|
||||
} else {
|
||||
self.1.as_mut()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_or_init<F: FnOnce() -> T>(&self, f: F) -> &T {
|
||||
let lock = self.0.lock();
|
||||
if self.1.is_none() {
|
||||
self.1 = Some(f());
|
||||
let mut lock = self.0.lock();
|
||||
if !*lock {
|
||||
unsafe {
|
||||
(core::ptr::addr_of!(self.1) as *mut Option<_>).write_unaligned(Some(f()));
|
||||
}
|
||||
}
|
||||
*lock = true;
|
||||
drop(lock);
|
||||
|
||||
self.1.as_ref().unwrap()
|
||||
self.get().unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user