aws-sdk-s3 1.135.0

AWS SDK for Amazon Simple Storage Service
Documentation
// Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! if-then-else (ite) function that takes in a bool and two (portentially optional)
//! values, choosing which one of those values to return based on the value
//! of the bool.

// Implementation Note: ite uses the same autoderef specialization trick as the coalesce
// macro. You can read more about how it works in the block comment above that macro.

/// Helper trait to implement the ite! macro
pub(crate) trait Ite {
    /// The first arg
    type Arg1;
    /// The second arg
    type Arg2;
    /// The result type
    type Result;

    /// Evaluates arguments, returns Arg1 if the boolean is true, and Arg2 if it is false
    fn ite(&self) -> fn(&bool, Self::Arg1, Self::Arg2) -> Self::Result;
}

impl<T> Ite for &&&&(&Option<T>, &Option<T>) {
    type Arg1 = Option<T>;
    type Arg2 = Option<T>;
    type Result = Option<T>;

    fn ite(&self) -> fn(&bool, Self::Arg1, Self::Arg2) -> Self::Result {
        |b: &bool, true_val: Option<T>, false_val: Option<T>| {
            if *b {
                true_val
            } else {
                false_val
            }
        }
    }
}

impl<T> Ite for &&&(&Option<T>, &T) {
    type Arg1 = Option<T>;
    type Arg2 = T;
    type Result = Option<T>;

    fn ite(&self) -> fn(&bool, Self::Arg1, Self::Arg2) -> Self::Result {
        |b: &bool, true_val: Option<T>, false_val: T| {
            if *b {
                true_val
            } else {
                Some(false_val)
            }
        }
    }
}

impl<T> Ite for &&(&T, &Option<T>) {
    type Arg1 = T;
    type Arg2 = Option<T>;
    type Result = Option<T>;

    fn ite(&self) -> fn(&bool, Self::Arg1, Self::Arg2) -> Self::Result {
        |b: &bool, true_val: T, false_val: Option<T>| {
            if *b {
                Some(true_val)
            } else {
                false_val
            }
        }
    }
}

impl<T> Ite for &(&T, &T) {
    type Arg1 = T;
    type Arg2 = T;
    type Result = T;

    fn ite(&self) -> fn(&bool, Self::Arg1, Self::Arg2) -> Self::Result {
        |b: &bool, true_val: T, false_val: T| {
            if *b {
                true_val
            } else {
                false_val
            }
        }
    }
}

macro_rules! ite {
    ($b:expr, $true_val:expr, $false_val:expr) => {{
        use crate::endpoint_lib::ite::Ite;
        let b = $b;
        let true_val = $true_val;
        let false_val = $false_val;
        (&&&&(&true_val, &false_val)).ite()(&b, true_val, false_val)
    }};
}

pub(crate) use ite;

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn base_cases() {
        // All types optional
        let a = "a";
        let some_a = Some("a");
        let b = "b";
        let some_b = Some("b");
        let none: Option<&str> = None;

        // Option, Option
        assert_eq!(ite!(true, some_a, some_b), Some("a"));
        assert_eq!(ite!(false, some_a, some_b), Some("b"));
        assert_eq!(ite!(false, some_a, none), None);
        assert_eq!(ite!(true, some_a, none), Some("a"));

        // Option, Value
        assert_eq!(ite!(true, some_a, b), Some("a"));
        assert_eq!(ite!(false, some_a, b), Some("b"));
        assert_eq!(ite!(true, none, b), None);
        assert_eq!(ite!(false, none, b), Some("b"));

        // Value, Option
        assert_eq!(ite!(true, a, some_b), Some("a"));
        assert_eq!(ite!(false, a, some_b), Some("b"));
        assert_eq!(ite!(false, a, none), None);
        assert_eq!(ite!(true, a, none), Some("a"));

        // Value, Value
        assert_eq!(ite!(true, a, b), "a");
        assert_eq!(ite!(false, a, b), "b");
    }
}