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 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
15use 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#[derive(Clone, Copy, Debug)]
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, c};
41        use core::mem;
42
43        #[test]
44        fn size_and_alignment() {
45            type Underlying = c::int;
46            assert_eq!(mem::size_of::<bssl::Result>(), mem::size_of::<Underlying>());
47            assert_eq!(
48                mem::align_of::<bssl::Result>(),
49                mem::align_of::<Underlying>()
50            );
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}