tuplities_replicate/lib.rs
1#![no_std]
2
3//! [tuplities](https://github.com/lucacappelletti94/tuplities) suite crate providing the `TupleReplicate` trait.
4
5#[tuplities_derive::impl_tuple_replicate]
6/// A trait for replicating a value into a tuple.
7///
8/// This trait allows creating a tuple where all elements are the same value.
9/// The implementation is optimized to avoid unnecessary cloning: for empty tuples
10/// and single-element tuples, no `Clone` bound is required. For tuples with 2+
11/// elements, `Clone` is required but the original value is moved to the last
12/// position to minimize clones.
13///
14/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
15pub trait TupleReplicate<T> {
16 /// Creates a tuple where all elements are the provided value.
17 ///
18 /// # Examples
19 ///
20 /// ```rust
21 /// use tuplities_replicate::TupleReplicate;
22 ///
23 /// let tuple: (i32, i32, i32) = TupleReplicate::tuple_replicate(42);
24 /// assert_eq!(tuple, (42, 42, 42));
25 /// let _tuple_empty: () = TupleReplicate::tuple_replicate(42);
26 /// let tuple_single: (i32,) = TupleReplicate::tuple_replicate(42);
27 /// assert_eq!(tuple_single, (42,));
28 /// let tuple_two: (i32, i32) = TupleReplicate::tuple_replicate(42);
29 /// assert_eq!(tuple_two, (42, 42));
30 ///
31 /// let tuple_refs: (&str, &str, &str) = TupleReplicate::tuple_replicate("hello");
32 /// assert_eq!(tuple_refs, ("hello", "hello", "hello"));
33 ///
34 /// let tuple_strings: (String, String) = TupleReplicate::tuple_replicate(String::from("world"));
35 /// assert_eq!(tuple_strings, (String::from("world"), String::from("world")));
36 ///
37 /// let tuple_refs2: (&i32, &i32) = TupleReplicate::tuple_replicate(&100);
38 /// assert_eq!(tuple_refs2, (&100, &100));
39 /// ```
40 fn tuple_replicate(value: T) -> Self;
41}