Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow implementing core::error::Error when available #184

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

vic1707
Copy link

@vic1707 vic1707 commented Sep 13, 2024

rust 1.81.0 made std::error::Error available in core.
Users with that version or higher should be getting the Error impl no matter if std feature is enabled.
fixes #179 without introducing a breaking change

@vic1707
Copy link
Author

vic1707 commented Sep 13, 2024

I think its good

lib.rs

use nutype::nutype;

#[nutype(validate(finite))]
struct A(f32);

Cargo.toml

[dependencies]
nutype = { path = "../nutype/nutype" }

Outputs of cargo expand

1.81.0

`nutype = { path = "../nutype/nutype" }`

=> impl ::core::error::Error for AError { /* ... */ }

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use nutype::nutype;
#[doc(hidden)]
mod __nutype_A__ {
    use super::*;
    pub struct A(f32);
    #[allow(clippy::enum_variant_names)]
    pub enum AError {
        FiniteViolated,
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::fmt::Debug for AError {
        #[inline]
        fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
            ::core::fmt::Formatter::write_str(f, "FiniteViolated")
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::clone::Clone for AError {
        #[inline]
        fn clone(&self) -> AError {
            AError::FiniteViolated
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::marker::StructuralPartialEq for AError {}
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::cmp::PartialEq for AError {
        #[inline]
        fn eq(&self, other: &AError) -> bool {
            true
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::cmp::Eq for AError {
        #[inline]
        #[doc(hidden)]
        #[coverage(off)]
        fn assert_receiver_is_total_eq(&self) -> () {}
    }
    impl ::core::fmt::Display for AError {
        fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
            match self {
                AError::FiniteViolated => {
                    f.write_fmt(format_args!("{0} is not finite.", "A"))
                }
            }
        }
    }
    impl ::core::error::Error for AError {
        fn source(&self) -> Option<&(dyn ::core::error::Error + 'static)> {
            None
        }
    }
    impl A {
        pub fn try_new(raw_value: f32) -> ::core::result::Result<Self, AError> {
            let sanitized_value: f32 = Self::__sanitize__(raw_value);
            Self::__validate__(&sanitized_value)?;
            Ok(A(sanitized_value))
        }
        fn __sanitize__(mut value: f32) -> f32 {
            value
        }
        fn __validate__(val: &f32) -> core::result::Result<(), AError> {
            let val = *val;
            if !val.is_finite() {
                return Err(AError::FiniteViolated);
            }
            Ok(())
        }
        #[deprecated(since = "0.4.3", note = "\nUse `try_new` instead.")]
        pub fn new(raw_value: f32) -> ::core::result::Result<Self, AError> {
            Self::try_new(raw_value)
        }
    }
    impl A {
        #[inline]
        pub fn into_inner(self) -> f32 {
            self.0
        }
    }
}
use __nutype_A__::A;
use __nutype_A__::AError;
`nutype = { path = "../nutype/nutype", default-features = false }`

=> impl ::core::error::Error for AError { /* ... */ }

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use nutype::nutype;
#[doc(hidden)]
mod __nutype_A__ {
    use super::*;
    pub struct A(f32);
    #[allow(clippy::enum_variant_names)]
    pub enum AError {
        FiniteViolated,
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::fmt::Debug for AError {
        #[inline]
        fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
            ::core::fmt::Formatter::write_str(f, "FiniteViolated")
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::clone::Clone for AError {
        #[inline]
        fn clone(&self) -> AError {
            AError::FiniteViolated
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::marker::StructuralPartialEq for AError {}
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::cmp::PartialEq for AError {
        #[inline]
        fn eq(&self, other: &AError) -> bool {
            true
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::cmp::Eq for AError {
        #[inline]
        #[doc(hidden)]
        #[coverage(off)]
        fn assert_receiver_is_total_eq(&self) -> () {}
    }
    impl ::core::fmt::Display for AError {
        fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
            match self {
                AError::FiniteViolated => {
                    f.write_fmt(format_args!("{0} is not finite.", "A"))
                }
            }
        }
    }
    impl ::core::error::Error for AError {
        fn source(&self) -> Option<&(dyn ::core::error::Error + 'static)> {
            None
        }
    }
    impl A {
        pub fn try_new(raw_value: f32) -> ::core::result::Result<Self, AError> {
            let sanitized_value: f32 = Self::__sanitize__(raw_value);
            Self::__validate__(&sanitized_value)?;
            Ok(A(sanitized_value))
        }
        fn __sanitize__(mut value: f32) -> f32 {
            value
        }
        fn __validate__(val: &f32) -> core::result::Result<(), AError> {
            let val = *val;
            if !val.is_finite() {
                return Err(AError::FiniteViolated);
            }
            Ok(())
        }
        #[deprecated(since = "0.4.3", note = "\nUse `try_new` instead.")]
        pub fn new(raw_value: f32) -> ::core::result::Result<Self, AError> {
            Self::try_new(raw_value)
        }
    }
    impl A {
        #[inline]
        pub fn into_inner(self) -> f32 {
            self.0
        }
    }
}
use __nutype_A__::A;
use __nutype_A__::AError;

1.80.0

`nutype = { path = "../nutype/nutype" }`

=> impl ::std::error::Error for AError { /* ... */ }

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use nutype::nutype;
#[doc(hidden)]
mod __nutype_A__ {
    use super::*;
    pub struct A(f32);
    #[allow(clippy::enum_variant_names)]
    pub enum AError {
        FiniteViolated,
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::fmt::Debug for AError {
        #[inline]
        fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
            ::core::fmt::Formatter::write_str(f, "FiniteViolated")
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::clone::Clone for AError {
        #[inline]
        fn clone(&self) -> AError {
            AError::FiniteViolated
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::marker::StructuralPartialEq for AError {}
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::cmp::PartialEq for AError {
        #[inline]
        fn eq(&self, other: &AError) -> bool {
            true
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::cmp::Eq for AError {
        #[inline]
        #[doc(hidden)]
        #[coverage(off)]
        fn assert_receiver_is_total_eq(&self) -> () {}
    }
    impl ::core::fmt::Display for AError {
        fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
            match self {
                AError::FiniteViolated => {
                    f.write_fmt(format_args!("{0} is not finite.", "A"))
                }
            }
        }
    }
    impl ::std::error::Error for AError {
        fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
            None
        }
    }
    impl A {
        pub fn try_new(raw_value: f32) -> ::core::result::Result<Self, AError> {
            let sanitized_value: f32 = Self::__sanitize__(raw_value);
            Self::__validate__(&sanitized_value)?;
            Ok(A(sanitized_value))
        }
        fn __sanitize__(mut value: f32) -> f32 {
            value
        }
        fn __validate__(val: &f32) -> core::result::Result<(), AError> {
            let val = *val;
            if !val.is_finite() {
                return Err(AError::FiniteViolated);
            }
            Ok(())
        }
        #[deprecated(since = "0.4.3", note = "\nUse `try_new` instead.")]
        pub fn new(raw_value: f32) -> ::core::result::Result<Self, AError> {
            Self::try_new(raw_value)
        }
    }
    impl A {
        #[inline]
        pub fn into_inner(self) -> f32 {
            self.0
        }
    }
}
use __nutype_A__::A;
use __nutype_A__::AError;
`nutype = { path = "../nutype/nutype", default-features = false }`

=> No implementation of Error

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use nutype::nutype;
#[doc(hidden)]
mod __nutype_A__ {
    use super::*;
    pub struct A(f32);
    #[allow(clippy::enum_variant_names)]
    pub enum AError {
        FiniteViolated,
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::fmt::Debug for AError {
        #[inline]
        fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
            ::core::fmt::Formatter::write_str(f, "FiniteViolated")
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::clone::Clone for AError {
        #[inline]
        fn clone(&self) -> AError {
            AError::FiniteViolated
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::marker::StructuralPartialEq for AError {}
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::cmp::PartialEq for AError {
        #[inline]
        fn eq(&self, other: &AError) -> bool {
            true
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::cmp::Eq for AError {
        #[inline]
        #[doc(hidden)]
        #[coverage(off)]
        fn assert_receiver_is_total_eq(&self) -> () {}
    }
    impl ::core::fmt::Display for AError {
        fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
            match self {
                AError::FiniteViolated => {
                    f.write_fmt(format_args!("{0} is not finite.", "A"))
                }
            }
        }
    }
    impl A {
        pub fn try_new(raw_value: f32) -> ::core::result::Result<Self, AError> {
            let sanitized_value: f32 = Self::__sanitize__(raw_value);
            Self::__validate__(&sanitized_value)?;
            Ok(A(sanitized_value))
        }
        fn __sanitize__(mut value: f32) -> f32 {
            value
        }
        fn __validate__(val: &f32) -> core::result::Result<(), AError> {
            let val = *val;
            if !val.is_finite() {
                return Err(AError::FiniteViolated);
            }
            Ok(())
        }
        #[deprecated(since = "0.4.3", note = "\nUse `try_new` instead.")]
        pub fn new(raw_value: f32) -> ::core::result::Result<Self, AError> {
            Self::try_new(raw_value)
        }
    }
    impl A {
        #[inline]
        pub fn into_inner(self) -> f32 {
            self.0
        }
    }
}
use __nutype_A__::A;
use __nutype_A__::AError;

1.83.0-nightly

`nutype = { path = "../nutype/nutype" }`

=> impl ::std::error::Error for AError { /* ... */ }

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use nutype::nutype;
#[doc(hidden)]
mod __nutype_A__ {
    use super::*;
    pub struct A(f32);
    #[allow(clippy::enum_variant_names)]
    pub enum AError {
        FiniteViolated,
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::fmt::Debug for AError {
        #[inline]
        fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
            ::core::fmt::Formatter::write_str(f, "FiniteViolated")
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::clone::Clone for AError {
        #[inline]
        fn clone(&self) -> AError {
            AError::FiniteViolated
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::marker::StructuralPartialEq for AError {}
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::cmp::PartialEq for AError {
        #[inline]
        fn eq(&self, other: &AError) -> bool {
            true
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::cmp::Eq for AError {
        #[inline]
        #[doc(hidden)]
        #[coverage(off)]
        fn assert_receiver_is_total_eq(&self) -> () {}
    }
    impl ::core::fmt::Display for AError {
        fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
            match self {
                AError::FiniteViolated => {
                    f.write_fmt(format_args!("{0} is not finite.", "A"))
                }
            }
        }
    }
    impl ::core::error::Error for AError {
        fn source(&self) -> Option<&(dyn ::core::error::Error + 'static)> {
            None
        }
    }
    impl A {
        pub fn try_new(raw_value: f32) -> ::core::result::Result<Self, AError> {
            let sanitized_value: f32 = Self::__sanitize__(raw_value);
            Self::__validate__(&sanitized_value)?;
            Ok(A(sanitized_value))
        }
        fn __sanitize__(mut value: f32) -> f32 {
            value
        }
        fn __validate__(val: &f32) -> core::result::Result<(), AError> {
            let val = *val;
            if !val.is_finite() {
                return Err(AError::FiniteViolated);
            }
            Ok(())
        }
        #[deprecated(since = "0.4.3", note = "\nUse `try_new` instead.")]
        pub fn new(raw_value: f32) -> ::core::result::Result<Self, AError> {
            Self::try_new(raw_value)
        }
    }
    impl A {
        #[inline]
        pub fn into_inner(self) -> f32 {
            self.0
        }
    }
}
use __nutype_A__::A;
use __nutype_A__::AError;
`nutype = { path = "../nutype/nutype", default-features = false }`

=> impl ::core::error::Error for AError { /* ... */ }

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use nutype::nutype;
#[doc(hidden)]
mod __nutype_A__ {
    use super::*;
    pub struct A(f32);
    #[allow(clippy::enum_variant_names)]
    pub enum AError {
        FiniteViolated,
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::fmt::Debug for AError {
        #[inline]
        fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
            ::core::fmt::Formatter::write_str(f, "FiniteViolated")
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::clone::Clone for AError {
        #[inline]
        fn clone(&self) -> AError {
            AError::FiniteViolated
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::marker::StructuralPartialEq for AError {}
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::cmp::PartialEq for AError {
        #[inline]
        fn eq(&self, other: &AError) -> bool {
            true
        }
    }
    #[automatically_derived]
    #[allow(clippy::enum_variant_names)]
    impl ::core::cmp::Eq for AError {
        #[inline]
        #[doc(hidden)]
        #[coverage(off)]
        fn assert_receiver_is_total_eq(&self) -> () {}
    }
    impl ::core::fmt::Display for AError {
        fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
            match self {
                AError::FiniteViolated => {
                    f.write_fmt(format_args!("{0} is not finite.", "A"))
                }
            }
        }
    }
    impl ::core::error::Error for AError {
        fn source(&self) -> Option<&(dyn ::core::error::Error + 'static)> {
            None
        }
    }
    impl A {
        pub fn try_new(raw_value: f32) -> ::core::result::Result<Self, AError> {
            let sanitized_value: f32 = Self::__sanitize__(raw_value);
            Self::__validate__(&sanitized_value)?;
            Ok(A(sanitized_value))
        }
        fn __sanitize__(mut value: f32) -> f32 {
            value
        }
        fn __validate__(val: &f32) -> core::result::Result<(), AError> {
            let val = *val;
            if !val.is_finite() {
                return Err(AError::FiniteViolated);
            }
            Ok(())
        }
        #[deprecated(since = "0.4.3", note = "\nUse `try_new` instead.")]
        pub fn new(raw_value: f32) -> ::core::result::Result<Self, AError> {
            Self::try_new(raw_value)
        }
    }
    impl A {
        #[inline]
        pub fn into_inner(self) -> f32 {
            self.0
        }
    }
}
use __nutype_A__::A;
use __nutype_A__::AError;

@vic1707 vic1707 marked this pull request as ready for review September 13, 2024 11:25
@greyblake
Copy link
Owner

greyblake commented Sep 17, 2024

@vic1707 Hi thanks for your PRs.
Just to keep you update: sorry for the delay.
I had seen them and wanted to review over the weekend by did not manage to.
I'll try to process them on the next weekend.

@vic1707
Copy link
Author

vic1707 commented Sep 17, 2024

No problem take your time 👍
Thanks for telling me

@vic1707
Copy link
Author

vic1707 commented Sep 18, 2024

Added the cargo expand on nightly just to be sure I didn't ruin everything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate core::error::Error for no_std
2 participants