| 1 | // Copyright 2015 Brian Smith. |
| 2 | // |
| 3 | // Permission to use, copy, modify, and/or distribute this software for any |
| 4 | // purpose with or without fee is hereby granted, provided that the above |
| 5 | // copyright notice and this permission notice appear in all copies. |
| 6 | // |
| 7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES |
| 8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 10 | // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | |
| 15 | use crate::{c, error}; |
| 16 | |
| 17 | /// An `int` returned from a foreign function containing **1** if the function |
| 18 | /// was successful or **0** if an error occurred. This is the convention used by |
| 19 | /// C code in `ring`. |
| 20 | #[must_use ] |
| 21 | #[repr (transparent)] |
| 22 | pub struct Result(c::int); |
| 23 | |
| 24 | impl From<Result> for core::result::Result<(), error::Unspecified> { |
| 25 | fn from(ret: Result) -> Self { |
| 26 | match ret.0 { |
| 27 | 1 => Ok(()), |
| 28 | c: i32 => { |
| 29 | debug_assert_eq!(c, 0, "`bssl::Result` value must be 0 or 1" ); |
| 30 | Err(error::Unspecified) |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | #[cfg (test)] |
| 37 | mod tests { |
| 38 | mod result { |
| 39 | use crate::{bssl, c}; |
| 40 | use core::mem::{align_of, size_of}; |
| 41 | |
| 42 | #[test ] |
| 43 | fn size_and_alignment() { |
| 44 | type Underlying = c::int; |
| 45 | assert_eq!(size_of::<bssl::Result>(), size_of::<Underlying>()); |
| 46 | assert_eq!(align_of::<bssl::Result>(), align_of::<Underlying>()); |
| 47 | } |
| 48 | |
| 49 | #[test ] |
| 50 | fn semantics() { |
| 51 | assert!(Result::from(bssl::Result(0)).is_err()); |
| 52 | assert!(Result::from(bssl::Result(1)).is_ok()); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |