Re: [PATCH v2 3/6] rust: error: Add Error::from_errno()

From: Martin Rodriguez Reboredo
Date: Wed Mar 29 2023 - 10:51:26 EST


On 3/29/23 09:04, Asahi Lina wrote:
> [...]
>
> impl Error {
> + /// Creates an [`Error`] from a kernel error code.
> + ///
> + /// It is a bug to pass an out-of-range `errno`. `EINVAL` would
> + /// be returned in such a case.
> + pub(crate) fn from_errno(errno: core::ffi::c_int) -> Error {
> + if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 {
> + // TODO: Make it a `WARN_ONCE` once available.
> + crate::pr_warn!(
> + "attempted to create `Error` with out of range `errno`: {}",
> + errno
> + );
> + return code::EINVAL;
> + }
> +
> + // INVARIANT: The check above ensures the type invariant
> + // will hold.
> + Error(errno)
> + }
> +
> /// Returns the kernel error code.
> pub fn to_errno(self) -> core::ffi::c_int {
> self.0
>

Reviewed-by: Martin Rodriguez Reboredo