~ireas/public-inbox

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch

[PATCH] Add strategies for strings

René Kijewski <rene.kijewski@fu-berlin.de>
Details
Message ID
<20220404085951.893753-1-rene.kijewski@fu-berlin.de>
DKIM signature
missing
Download raw message
Patch: +64 -0
This is simply a copy of vector's strategies.
---
 src/lib.rs          | 24 ++++++++++++++++++++++++
 tests/strategies.rs | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)

diff --git a/src/lib.rs b/src/lib.rs
index ab9b4ab..2640107 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -299,3 +299,27 @@ pub mod hashmap {
        }
    }
}

/// Merge strategies for strings.
///
/// These strategies are only available if the `std` feature is enabled.
#[cfg(feature = "std")]
pub mod string {
    /// Overwrite left with right if left is empty.
    pub fn overwrite_empty(left: &mut String, right: String) {
        if left.is_empty() {
            *left = right;
        }
    }

    /// Append the contents of right to left.
    pub fn append(left: &mut String, mut right: String) {
        left.push_str(&mut right);
    }

    /// Prepend the contents of right to left.
    pub fn prepend(left: &mut String, mut right: String) {
        right.push_str(left);
        *left = right;
    }
}
diff --git a/tests/strategies.rs b/tests/strategies.rs
index 1113f3e..aaade78 100644
--- a/tests/strategies.rs
+++ b/tests/strategies.rs
@@ -214,3 +214,43 @@ mod hashmap {
        );
    }
}

#[cfg(feature = "std")]
#[test]
fn test_string_overwrite_empty() {
    #[derive(Debug, Merge, PartialEq)]
    struct S(#[merge(strategy = merge::string::overwrite_empty)] String);

    test(S("".to_owned()), S("".to_owned()), S("".to_owned()));
    test(S("1".to_owned()), S("".to_owned()), S("1".to_owned()));
    test(S("0".to_owned()), S("0".to_owned()), S("1".to_owned()));
    test(S("255".to_owned()), S("255".to_owned()), S("10".to_owned()));
}

#[cfg(feature = "std")]
#[test]
fn test_string_append() {
    #[derive(Debug, Merge, PartialEq)]
    struct S(#[merge(strategy = merge::string::append)] String);

    test(S("".to_owned()), S("".to_owned()), S("".to_owned()));
    test(S("1".to_owned()), S("".to_owned()), S("1".to_owned()));
    test(S("01".to_owned()), S("0".to_owned()), S("1".to_owned()));
    test(S("25510".to_owned()), S("255".to_owned()), S("10".to_owned()));
    test(S("01234".to_owned()), S("012".to_owned()), S("34".to_owned()));
    test(S("34012".to_owned()), S("34".to_owned()), S("012".to_owned()));
}

#[cfg(feature = "std")]
#[test]
fn test_string_prepend() {
    #[derive(Debug, Merge, PartialEq)]
    struct S(#[merge(strategy = merge::string::prepend)] String);

    test(S("".to_owned()), S("".to_owned()), S("".to_owned()));
    test(S("1".to_owned()), S("".to_owned()), S("1".to_owned()));
    test(S("10".to_owned()), S("0".to_owned()), S("1".to_owned()));
    test(S("10255".to_owned()), S("255".to_owned()), S("10".to_owned()));
    test(S("34012".to_owned()), S("012".to_owned()), S("34".to_owned()));
    test(S("01234".to_owned()), S("34".to_owned()), S("012".to_owned()));
}
-- 
2.35.1
Reply to thread Export thread (mbox)