Skip to content

Commit

Permalink
Merge pull request #7 from alexkazik/tuple
Browse files Browse the repository at this point in the history
Tuple
  • Loading branch information
alexkazik authored Sep 20, 2023
2 parents e53d223 + 320424f commit 0fdeee7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ownable/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ownable"
version = "0.6.0"
version = "0.6.1"
authors = ["ALeX Kazik <alex@kazik.de>"]
edition = "2021"
description = "Derive macro for structs/enums with Cow, which can convert Type<'a> to Type<'static> and more"
Expand Down
4 changes: 4 additions & 0 deletions ownable/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for ownable

## 0.6.1 -- 2023-09-20

* Support tuples

## 0.6.0 -- 2023-05-06

* Support to not generate functions
Expand Down
52 changes: 52 additions & 0 deletions ownable/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,55 @@ where
.collect()
}
}

// Tuples

macro_rules! tuple_impls {
($($(#[$attrs:meta])?($($n:tt $name:ident)+),)+) => {
$(
impl<'a, $($name: ToBorrowed<'a>),+> ToBorrowed<'a> for ($($name,)+) {
$(#[$attrs])?
fn to_borrowed(&'a self) -> Self {
($(self.$n.to_borrowed(),)+)
}
}

impl<$($name: ToOwned),+> ToOwned for ($($name,)+) {
type Owned = ($($name::Owned,)+);

$(#[$attrs])?
fn to_owned(&self) -> Self::Owned where {
($(self.$n.to_owned(),)+)
}
}

impl<$($name: IntoOwned),+> IntoOwned for ($($name,)+) {
type Owned = ($($name::Owned,)+);

$(#[$attrs])?
fn into_owned(self) -> Self::Owned {
($(IntoOwned::into_owned(self.$n),)+)
}
}
)+
};
}

tuple_impls! {
#[inline(always)] (0 T0),
#[inline] (0 T0 1 T1),
#[inline] (0 T0 1 T1 2 T2),
(0 T0 1 T1 2 T2 3 T3),
(0 T0 1 T1 2 T2 3 T3 4 T4),
(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5),
(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6),
(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7),
(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8),
(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9),
(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10),
(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11),
(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12),
(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13),
(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14),
(0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15),
}
2 changes: 2 additions & 0 deletions ownable/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct Test<'a> {
copy: usize,
as_copy: AsCopy<Ipv4Addr>,
as_clone: AsClone<String>,
tuple: (Cow<'a, str>, u64),
}

#[test]
Expand All @@ -23,6 +24,7 @@ fn test() {
copy: 0,
as_copy: AsCopy(Ipv4Addr::LOCALHOST),
as_clone: AsClone("as_clone".to_string()),
tuple: (Cow::Borrowed(&value), 64),
};
let v1: Test<'_> = v0.to_borrowed();
assert_eq!(v0, v1);
Expand Down

0 comments on commit 0fdeee7

Please sign in to comment.