~ireas/public-inbox

Add strategies for strings v1 PROPOSED

René Kijewski: 1
 Add strategies for strings

 2 files changed, 64 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~ireas/public-inbox/patches/30792/mbox | git am -3
Learn more about email & git

[PATCH] Add strategies for strings Export this patch

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