std_shims/
lib.rs

1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2#![doc = include_str!("../README.md")]
3#![cfg_attr(not(feature = "std"), no_std)]
4
5#[cfg(not(feature = "alloc"))]
6pub use core::*;
7#[cfg(not(feature = "alloc"))]
8pub use core::{alloc, borrow, ffi, fmt, slice, str, task};
9
10#[cfg(not(feature = "std"))]
11#[rustversion::before(1.81)]
12pub mod error {
13  use core::fmt::Debug::Display;
14  pub trait Error: Debug + Display {}
15}
16#[cfg(not(feature = "std"))]
17#[rustversion::since(1.81)]
18pub use core::error;
19
20#[cfg(all(feature = "alloc", not(feature = "std")))]
21pub extern crate alloc as extern_alloc;
22#[cfg(all(feature = "alloc", not(feature = "std")))]
23pub use extern_alloc::{alloc, borrow, boxed, ffi, fmt, rc, slice, str, string, task, vec, format};
24#[cfg(feature = "std")]
25pub use std::{alloc, borrow, boxed, error, ffi, fmt, rc, slice, str, string, task, vec, format};
26
27pub mod collections;
28pub mod io;
29pub mod sync;
30
31pub mod prelude {
32  // Shim the `std` prelude
33  #[cfg(all(feature = "alloc", not(feature = "std")))]
34  pub use extern_alloc::{
35    format, vec,
36    borrow::ToOwned,
37    boxed::Box,
38    vec::Vec,
39    string::{String, ToString},
40  };
41
42  // Shim `div_ceil`
43  #[rustversion::before(1.73)]
44  #[doc(hidden)]
45  pub trait StdShimsDivCeil {
46    fn div_ceil(self, rhs: Self) -> Self;
47  }
48  #[rustversion::before(1.73)]
49  mod impl_divceil {
50    use super::StdShimsDivCeil;
51    impl StdShimsDivCeil for u8 {
52      fn div_ceil(self, rhs: Self) -> Self {
53        (self + (rhs - 1)) / rhs
54      }
55    }
56    impl StdShimsDivCeil for u16 {
57      fn div_ceil(self, rhs: Self) -> Self {
58        (self + (rhs - 1)) / rhs
59      }
60    }
61    impl StdShimsDivCeil for u32 {
62      fn div_ceil(self, rhs: Self) -> Self {
63        (self + (rhs - 1)) / rhs
64      }
65    }
66    impl StdShimsDivCeil for u64 {
67      fn div_ceil(self, rhs: Self) -> Self {
68        (self + (rhs - 1)) / rhs
69      }
70    }
71    impl StdShimsDivCeil for u128 {
72      fn div_ceil(self, rhs: Self) -> Self {
73        (self + (rhs - 1)) / rhs
74      }
75    }
76    impl StdShimsDivCeil for usize {
77      fn div_ceil(self, rhs: Self) -> Self {
78        (self + (rhs - 1)) / rhs
79      }
80    }
81  }
82
83  // Shim `io::Error::other`
84  #[cfg(feature = "std")]
85  #[rustversion::before(1.74)]
86  #[doc(hidden)]
87  pub trait StdShimsIoErrorOther {
88    fn other<E>(error: E) -> Self
89    where
90      E: Into<Box<dyn std::error::Error + Send + Sync>>;
91  }
92  #[cfg(feature = "std")]
93  #[rustversion::before(1.74)]
94  impl StdShimsIoErrorOther for std::io::Error {
95    fn other<E>(error: E) -> Self
96    where
97      E: Into<Box<dyn std::error::Error + Send + Sync>>,
98    {
99      std::io::Error::new(std::io::ErrorKind::Other, error)
100    }
101  }
102}