ring/
bssl.rs

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 AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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
15use crate::error;
16use core::ffi::c_int;
17
18/// An `int` returned from a foreign function containing **1** if the function
19/// was successful or **0** if an error occurred. This is the convention used by
20/// C code in `ring`.
21#[must_use]
22#[repr(transparent)]
23pub struct Result(c_int);
24
25impl From<Result> for core::result::Result<(), error::Unspecified> {
26    fn from(ret: Result) -> Self {
27        match ret.0 {
28            1 => Ok(()),
29            c => {
30                debug_assert_eq!(c, 0, "`bssl::Result` value must be 0 or 1");
31                Err(error::Unspecified)
32            }
33        }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    mod result {
40        use crate::bssl;
41        use core::{
42            ffi::c_int,
43            mem::{align_of, size_of},
44        };
45
46        #[test]
47        fn size_and_alignment() {
48            type Underlying = c_int;
49            assert_eq!(size_of::<bssl::Result>(), size_of::<Underlying>());
50            assert_eq!(align_of::<bssl::Result>(), align_of::<Underlying>());
51        }
52
53        #[test]
54        fn semantics() {
55            assert!(Result::from(bssl::Result(0)).is_err());
56            assert!(Result::from(bssl::Result(1)).is_ok());
57        }
58    }
59}