Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Commit

Permalink
rustdoc examples for svm_result_t.
Browse files Browse the repository at this point in the history
  • Loading branch information
YaronWittenstein committed Feb 20, 2020
1 parent 798b7f7 commit 6084289
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/svm-runtime-c-api/src/import.rs
Expand Up @@ -2,7 +2,7 @@ use std::{ffi::c_void, ptr::NonNull};

use crate::svm_value_type;

/// Represents an `Import`` kind
/// Represents an `Import` kind
#[allow(non_camel_case_types)]
pub enum svm_import_kind {
#[doc(hidden)]
Expand Down
2 changes: 1 addition & 1 deletion crates/svm-runtime-c-api/src/macros.rs
Expand Up @@ -27,7 +27,7 @@ macro_rules! impl_into_svm_byte_array {
impl From<$struct> for $crate::svm_byte_array {
fn from(value: $struct) -> Self {
// `bytes` is a copy of the underlying bytes.
let bytes = value.bytes();
let bytes = value.bytes);

debug_assert_eq!($struct::len(), bytes.len());

Expand Down
48 changes: 43 additions & 5 deletions crates/svm-runtime-c-api/src/result.rs
Expand Up @@ -10,10 +10,23 @@ pub enum svm_result_t {
SVM_FAILURE = 1,
}

impl Into<bool> for svm_result_t {
///
/// # Example
///
/// ```rust
/// use svm_runtime_c_api::svm_result_t;
///
/// let truthy = svm_result_t::SVM_SUCCESS;
/// let falsey = svm_result_t::SVM_FAILURE;
///
/// assert_eq!(true, bool::from(truthy));
/// assert_eq!(false, bool::from(falsey));
/// ```
///
impl From<svm_result_t> for bool {
#[inline]
fn into(self) -> bool {
match self {
fn from(value: svm_result_t) -> bool {
match value {
svm_result_t::SVM_SUCCESS => true,
svm_result_t::SVM_FAILURE => false,
}
Expand All @@ -22,20 +35,45 @@ impl Into<bool> for svm_result_t {

impl svm_result_t {
/// Returns whether equals to `svm_result::SVM_SUCCESS`
///
/// # Example
///
/// ```rust
/// use svm_runtime_c_api::svm_result_t;
///
/// let truthy = svm_result_t::SVM_SUCCESS;
/// let falsey = svm_result_t::SVM_FAILURE;
///
/// assert!(truthy.is_ok());
/// assert!(!falsey.is_ok());
/// ```
///
#[inline]
pub fn is_ok(self) -> bool {
self.as_bool() == true
}

/// Returns whether equals to `svm_result::SVM_FAILURE`
///
/// # Example
///
/// ```rust
/// use svm_runtime_c_api::svm_result_t;
///
/// let truthy = svm_result_t::SVM_SUCCESS;
/// let falsey = svm_result_t::SVM_FAILURE;
///
/// assert!(!truthy.is_err());
/// assert!(falsey.is_err());
/// ```
#[inline]
pub fn is_err(self) -> bool {
self.as_bool() == false
}

/// Convert to boolean
/// Convert to a boolean
#[inline]
pub fn as_bool(self) -> bool {
fn as_bool(self) -> bool {
self.into()
}
}

0 comments on commit 6084289

Please sign in to comment.