Skip to content
Myr

Merge pull request #383 from dtolnay/nostd

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Support error sources in no-std on Rust 1.81+

authored by David Tolnay · committed by GitHub · parents 1eabf69, bee814a · Browse files · 9 files+64 added−58 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+64 added−58 removed

  • other:+64 added−58 removed

9 files+64 added−58 removed

modified:.github/workflows/ci.yml+1 added-1 removed
modified:src/backtrace.rs+6 added-2 removed
45 unmodified lines
46 };47}4849#[cfg(all(50 feature = "std",50 any(feature = "std", not(anyhow_no_core_error)),51 not(error_generic_member_access),52 any(std_backtrace, feature = "backtrace")53))]54macro_rules! backtrace_if_absent {
1 unmodified line
56 backtrace!()57 };58}5960#[cfg(all(feature = "std", not(std_backtrace), not(feature = "backtrace")))]60#[cfg(all(61 any(feature = "std", not(anyhow_no_core_error)),62 not(std_backtrace),63 not(feature = "backtrace"),64))]65macro_rules! backtrace_if_absent {66 ($err:expr) => {67 None68 };
343 unmodified lines
modified:src/chain.rs+8 added-8 removed
1use self::ChainState::*;2use crate::StdError;34#[cfg(feature = "std")]4#[cfg(any(feature = "std", not(anyhow_no_core_error)))]5use alloc::vec::{self, Vec};67#[cfg(feature = "std")]7#[cfg(any(feature = "std", not(anyhow_no_core_error)))]8pub(crate) use crate::Chain;910#[cfg(not(feature = "std"))]10#[cfg(all(not(feature = "std"), anyhow_no_core_error))]11pub(crate) struct Chain<'a> {12 state: ChainState<'a>,13}14
1 unmodified line
16pub(crate) enum ChainState<'a> {17 Linked {18 next: Option<&'a (dyn StdError + 'static)>,19 },20 #[cfg(feature = "std")]20 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]21 Buffered {22 rest: vec::IntoIter<&'a (dyn StdError + 'static)>,23 },24}
16 unmodified lines
41 let error = (*next)?;42 *next = error.source();43 Some(error)44 }45 #[cfg(feature = "std")]45 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]46 Buffered { rest } => rest.next(),47 }48 }49
2 unmodified lines
52 (len, Some(len))53 }54}5556#[cfg(feature = "std")]56#[cfg(any(feature = "std", not(anyhow_no_core_error)))]57impl DoubleEndedIterator for Chain<'_> {58 fn next_back(&mut self) -> Option<Self::Item> {59 match &mut self.state {60 Linked { mut next } => {
22 unmodified lines
83 len += 1;84 }85 len86 }87 #[cfg(feature = "std")]87 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]88 Buffered { rest } => rest.len(),89 }90 }91}9293#[cfg(feature = "std")]93#[cfg(any(feature = "std", not(anyhow_no_core_error)))]94impl Default for Chain<'_> {95 fn default() -> Self {96 Chain {97 state: ChainState::Buffered {
5 unmodified lines
modified:src/context.rs+2 added-2 removed
13 unmodified lines
14 where15 C: Display + Send + Sync + 'static;16 }1718 #[cfg(feature = "std")]18 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]19 impl<E> StdError for E20 where21 E: std::error::Error + Send + Sync + 'static,21 E: crate::StdError + Send + Sync + 'static,22 {23 fn ext_context<C>(self, context: C) -> Error24 where25 C: Display + Send + Sync + 'static,
168 unmodified lines
modified:src/error.rs+22 added-29 removed
1use crate::backtrace::Backtrace;2use crate::chain::Chain;3#[cfg(any(feature = "std", anyhow_no_ptr_addr_of))]3#[cfg(any(feature = "std", not(anyhow_no_core_error), anyhow_no_ptr_addr_of))]4use crate::ptr::Mut;5use crate::ptr::{Own, Ref};6use crate::{Error, StdError};7use alloc::boxed::Box;
1 unmodified line
9#[cfg(error_generic_member_access)]10use core::error::{self, Request};11use core::fmt::{self, Debug, Display};12use core::mem::ManuallyDrop;13#[cfg(any(feature = "std", not(anyhow_no_core_error)))]14use core::ops::{Deref, DerefMut};15#[cfg(not(anyhow_no_ptr_addr_of))]16use core::ptr;17use core::ptr::NonNull;1817#[cfg(feature = "std")]18use core::ops::{Deref, DerefMut};1919impl Error {20 /// Create a new error object from any error type.21 ///22 /// The error type must be threadsafe and `'static`, so that the `Error`23 /// will be as well.24 ///25 /// If the error type does not provide a backtrace, a backtrace will be26 /// created here to ensure that a backtrace exists.28 #[cfg(feature = "std")]29 #[cfg_attr(docsrs, doc(cfg(feature = "std")))]27 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]28 #[cold]29 #[must_use]30 pub fn new<E>(error: E) -> Self31 where
48 unmodified lines
80 {81 Error::from_adhoc(message, backtrace!())82 }8386 #[cfg(feature = "std")]84 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]85 #[cold]86 pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self87 where88 E: StdError + Send + Sync + 'static,
28 unmodified lines
117 let error: MessageError<M> = MessageError(message);118 let vtable = &ErrorVTable {119 object_drop: object_drop::<MessageError<M>>,120 object_ref: object_ref::<MessageError<M>>,123 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]121 #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]122 object_mut: object_mut::<MessageError<M>>,123 object_boxed: object_boxed::<MessageError<M>>,124 object_downcast: object_downcast::<M>,125 #[cfg(anyhow_no_ptr_addr_of)]
20 unmodified lines
146 let error: DisplayError<M> = DisplayError(message);147 let vtable = &ErrorVTable {148 object_drop: object_drop::<DisplayError<M>>,149 object_ref: object_ref::<DisplayError<M>>,152 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]150 #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]151 object_mut: object_mut::<DisplayError<M>>,152 object_boxed: object_boxed::<DisplayError<M>>,153 object_downcast: object_downcast::<M>,154 #[cfg(anyhow_no_ptr_addr_of)]
10 unmodified lines
165 // vtable to allow casting the DisplayError<M> to M.166 unsafe { Error::construct(error, vtable, backtrace) }167 }168171 #[cfg(feature = "std")]169 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]170 #[cold]171 pub(crate) fn from_context<C, E>(context: C, error: E, backtrace: Option<Backtrace>) -> Self172 where173 C: Display + Send + Sync + 'static,
21 unmodified lines
195 // Safety: passing vtable that operates on the right type.196 unsafe { Error::construct(error, vtable, backtrace) }197 }198201 #[cfg(feature = "std")]199 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]200 #[cold]201 pub(crate) fn from_boxed(202 error: Box<dyn StdError + Send + Sync>,203 backtrace: Option<Backtrace>,
118 unmodified lines
322323 let vtable = &ErrorVTable {324 object_drop: object_drop::<ContextError<C, Error>>,325 object_ref: object_ref::<ContextError<C, Error>>,328 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]326 #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]327 object_mut: object_mut::<ContextError<C, Error>>,328 object_boxed: object_boxed::<ContextError<C, Error>>,329 object_downcast: context_chain_downcast::<C>,330 #[cfg(anyhow_no_ptr_addr_of)]
65 unmodified lines
396 /// }397 /// None398 /// }399 /// ```402 #[cfg(feature = "std")]403 #[cfg_attr(docsrs, doc(cfg(feature = "std")))]400 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]401 #[cold]402 pub fn chain(&self) -> Chain {403 unsafe { ErrorImpl::chain(self.inner.by_ref()) }404 }
2 unmodified lines
407 /// cause's cause etc.408 ///409 /// The root cause is the last error in the iterator produced by410 /// [`chain()`][Error::chain].414 #[cfg(feature = "std")]415 #[cfg_attr(docsrs, doc(cfg(feature = "std")))]411 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]412 pub fn root_cause(&self) -> &(dyn StdError + 'static) {413 self.chain().last().unwrap()414 }415
133 unmodified lines
549 Self::provide(self, request);550 }551}552557#[cfg(feature = "std")]558#[cfg_attr(docsrs, doc(cfg(feature = "std")))]553#[cfg(any(feature = "std", not(anyhow_no_core_error)))]554impl<E> From<E> for Error555where556 E: StdError + Send + Sync + 'static,557{
3 unmodified lines
561 Error::from_std(error, backtrace)562 }563}564570#[cfg(feature = "std")]571#[cfg_attr(docsrs, doc(cfg(feature = "std")))]565#[cfg(any(feature = "std", not(anyhow_no_core_error)))]566impl Deref for Error {567 type Target = dyn StdError + Send + Sync + 'static;568569 fn deref(&self) -> &Self::Target {570 unsafe { ErrorImpl::error(self.inner.by_ref()) }571 }572}573580#[cfg(feature = "std")]581#[cfg_attr(docsrs, doc(cfg(feature = "std")))]574#[cfg(any(feature = "std", not(anyhow_no_core_error)))]575impl DerefMut for Error {576 fn deref_mut(&mut self) -> &mut Self::Target {577 unsafe { ErrorImpl::error_mut(self.inner.by_mut()) }578 }
22 unmodified lines
601602struct ErrorVTable {603 object_drop: unsafe fn(Own<ErrorImpl>),604 object_ref: unsafe fn(Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>,612 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]605 #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]606 object_mut: unsafe fn(Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static),607 object_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,608 object_downcast: unsafe fn(Ref<ErrorImpl>, TypeId) -> Option<Ref<()>>,609 #[cfg(anyhow_no_ptr_addr_of)]
43 unmodified lines
653}654655// Safety: requires layout of *e to match ErrorImpl<E>, and for `e` to be derived656// from a `&mut`664#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]657#[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]658unsafe fn object_mut<E>(e: Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static)659where660 E: StdError + Send + Sync + 'static,661{
64 unmodified lines
726 None727}728729// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.737#[cfg(feature = "std")]730#[cfg(any(feature = "std", not(anyhow_no_core_error)))]731unsafe fn context_downcast<C, E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>732where733 C: 'static,734 E: 'static,
31 unmodified lines
766 }767}768769// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.777#[cfg(feature = "std")]770#[cfg(any(feature = "std", not(anyhow_no_core_error)))]771unsafe fn context_drop_rest<C, E>(e: Own<ErrorImpl>, target: TypeId)772where773 C: 'static,774 E: 'static,
123 unmodified lines
898 // original type E.899 unsafe { (vtable(this.ptr).object_ref)(this).deref() }900 }901909 #[cfg(feature = "std")]902 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]903 pub(crate) unsafe fn error_mut(this: Mut<Self>) -> &mut (dyn StdError + Send + Sync + 'static) {904 // Use vtable to attach E's native StdError vtable for the right905 // original type E.906
94 unmodified lines
1001 Box::<dyn StdError + Send + Sync>::from(error)1002 }1003}10041012#[cfg(feature = "std")]1005#[cfg(any(feature = "std", not(anyhow_no_core_error)))]1006impl AsRef<dyn StdError + Send + Sync> for Error {1007 fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {1008 &**self1009 }1010}10111019#[cfg(feature = "std")]1012#[cfg(any(feature = "std", not(anyhow_no_core_error)))]1013impl AsRef<dyn StdError> for Error {1014 fn as_ref(&self) -> &(dyn StdError + 'static) {1015 &**self1016 }
1 unmodified line
modified:src/kind.rs+6 added-6 removed
45 unmodified lines
4647use crate::Error;48use core::fmt::{Debug, Display};4950#[cfg(feature = "std")]50#[cfg(any(feature = "std", not(anyhow_no_core_error)))]51use crate::StdError;52#[cfg(feature = "std")]52#[cfg(any(feature = "std", not(anyhow_no_core_error)))]53use alloc::boxed::Box;5455pub struct Adhoc;56
38 unmodified lines
95 error.into()96 }97}9899#[cfg(feature = "std")]99#[cfg(any(feature = "std", not(anyhow_no_core_error)))]100pub struct Boxed;101102#[cfg(feature = "std")]102#[cfg(any(feature = "std", not(anyhow_no_core_error)))]103#[doc(hidden)]104pub trait BoxedKind: Sized {105 #[inline]106 fn anyhow_kind(&self) -> Boxed {107 Boxed108 }109}110111#[cfg(feature = "std")]111#[cfg(any(feature = "std", not(anyhow_no_core_error)))]112impl BoxedKind for Box<dyn StdError + Send + Sync> {}113114#[cfg(feature = "std")]114#[cfg(any(feature = "std", not(anyhow_no_core_error)))]115impl Boxed {116 #[cold]117 pub fn new(self, error: Box<dyn StdError + Send + Sync>) -> Error {118 let backtrace = backtrace_if_absent!(&*error);
3 unmodified lines
modified:src/lib.rs+7 added-5 removed
264 unmodified lines
265use crate::error::ErrorImpl;266use crate::ptr::Own;267use core::fmt::Display;268269#[cfg(not(feature = "std"))]269#[cfg(all(not(feature = "std"), anyhow_no_core_error))]270use core::fmt::Debug;271272#[cfg(feature = "std")]273use std::error::Error as StdError;274275#[cfg(not(feature = "std"))]275#[cfg(not(any(feature = "std", anyhow_no_core_error)))]276use core::error::Error as StdError;277278#[cfg(all(not(feature = "std"), anyhow_no_core_error))]279trait StdError: Debug + Display {280 fn source(&self) -> Option<&(dyn StdError + 'static)> {281 None282 }
126 unmodified lines
409/// }410/// None411/// }412/// ```410#[cfg(feature = "std")]411#[cfg_attr(docsrs, doc(cfg(feature = "std")))]413#[cfg(any(feature = "std", not(anyhow_no_core_error)))]414#[derive(Clone)]415pub struct Chain<'a> {416 state: crate::chain::ChainState<'a>,417}
253 unmodified lines
671 pub mod kind {672 #[doc(hidden)]673 pub use crate::kind::{AdhocKind, TraitKind};674673 #[cfg(feature = "std")]675 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]676 #[doc(hidden)]677 pub use crate::kind::BoxedKind;678 }679
53 unmodified lines
modified:src/wrapper.rs+5 added-5 removed
1use crate::StdError;2use core::fmt::{self, Debug, Display};34#[cfg(feature = "std")]4#[cfg(any(feature = "std", not(anyhow_no_core_error)))]5use alloc::boxed::Box;67#[cfg(error_generic_member_access)]8use core::error::Request;
43 unmodified lines
52}5354impl<M> StdError for DisplayError<M> where M: Display + 'static {}5556#[cfg(feature = "std")]56#[cfg(any(feature = "std", not(anyhow_no_core_error)))]57#[repr(transparent)]58pub struct BoxedError(pub Box<dyn StdError + Send + Sync>);5960#[cfg(feature = "std")]60#[cfg(any(feature = "std", not(anyhow_no_core_error)))]61impl Debug for BoxedError {62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {63 Debug::fmt(&self.0, f)64 }65}6667#[cfg(feature = "std")]67#[cfg(any(feature = "std", not(anyhow_no_core_error)))]68impl Display for BoxedError {69 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {70 Display::fmt(&self.0, f)71 }72}7374#[cfg(feature = "std")]74#[cfg(any(feature = "std", not(anyhow_no_core_error)))]75impl StdError for BoxedError {76 fn source(&self) -> Option<&(dyn StdError + 'static)> {77 self.0.source()78 }
6 unmodified lines
modified:build.rs+7 added
65 unmodified lines
66 };6768 if rustc >= 80 {69 println!("cargo:rustc-check-cfg=cfg(anyhow_nightly_testing)");70 println!("cargo:rustc-check-cfg=cfg(anyhow_no_core_error)");71 println!("cargo:rustc-check-cfg=cfg(anyhow_no_fmt_arguments_as_str)");72 println!("cargo:rustc-check-cfg=cfg(anyhow_no_ptr_addr_of)");73 println!("cargo:rustc-check-cfg=cfg(anyhow_no_unsafe_op_in_unsafe_fn_lint)");74 println!("cargo:rustc-check-cfg=cfg(error_generic_member_access)");
20 unmodified lines
95 // std::backtrace::Backtrace96 // https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#stabilized-apis97 println!("cargo:rustc-cfg=std_backtrace");98 }99100 if rustc < 81 {101 // core::error::Error102 // https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#coreerrorerror103 println!("cargo:rustc-cfg=anyhow_no_core_error");104 }105}106107fn compile_probe(rustc_bootstrap: bool) -> bool {108 if env::var_os("RUSTC_STAGE").is_some() {
77 unmodified lines