From e4844d482cd16cbee031cd23aa291a24e8078a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Mon, 27 May 2024 11:15:46 +0200 Subject: [PATCH 01/10] feat: impl Into for Span/Array pair --- corelib/src/array.cairo | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/corelib/src/array.cairo b/corelib/src/array.cairo index 646f6c57b37..7735d96a739 100644 --- a/corelib/src/array.cairo +++ b/corelib/src/array.cairo @@ -146,6 +146,22 @@ pub struct Span { impl SpanCopy of Copy>; impl SpanDrop of Drop>; +impl ArrayIntoSpan> of Into, Span> { + fn into(self: Array) -> Span { + self.span() + } +} + +impl SpanIntoArray, +Clone> of Into, Array> { + fn into(mut self: Span) -> Array { + let mut arr = array![]; + + arr.append_span(self); + + arr + } +} + impl SpanFelt252Serde of Serde> { fn serialize(self: @Span, ref output: Array) { (*self).len().serialize(ref output); From 06b5039776da09659d2774f842dcfaafa54e8243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Mon, 27 May 2024 15:50:21 +0200 Subject: [PATCH 02/10] remove empty lines --- corelib/src/array.cairo | 2 -- 1 file changed, 2 deletions(-) diff --git a/corelib/src/array.cairo b/corelib/src/array.cairo index 7735d96a739..a2cb8b1fa8b 100644 --- a/corelib/src/array.cairo +++ b/corelib/src/array.cairo @@ -155,9 +155,7 @@ impl ArrayIntoSpan> of Into, Span> { impl SpanIntoArray, +Clone> of Into, Array> { fn into(mut self: Span) -> Array { let mut arr = array![]; - arr.append_span(self); - arr } } From 006803df5974bed8eac9acbe6f21b7d0f3607821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Mon, 27 May 2024 15:54:25 +0200 Subject: [PATCH 03/10] add: impl ArraySnapIntoSpan --- corelib/src/array.cairo | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/corelib/src/array.cairo b/corelib/src/array.cairo index a2cb8b1fa8b..48dd7bd29af 100644 --- a/corelib/src/array.cairo +++ b/corelib/src/array.cairo @@ -152,6 +152,12 @@ impl ArrayIntoSpan> of Into, Span> { } } +impl ArraySnapIntoSpan of Into<@Array, Span> { + fn into(self: @Array) -> Span { + self.span() + } +} + impl SpanIntoArray, +Clone> of Into, Array> { fn into(mut self: Span) -> Array { let mut arr = array![]; From 4978798c4027fd26249a7ca116b65e239fd19283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Mon, 27 May 2024 15:58:47 +0200 Subject: [PATCH 04/10] impl SpanIntoArraySnap --- corelib/src/array.cairo | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/corelib/src/array.cairo b/corelib/src/array.cairo index 48dd7bd29af..2262071f28a 100644 --- a/corelib/src/array.cairo +++ b/corelib/src/array.cairo @@ -166,6 +166,12 @@ impl SpanIntoArray, +Clone> of Into, Array> { } } +impl SpanIntoArraySnap of Into, @Array> { + fn into(self: Span) -> @Array { + self.snapshot + } +} + impl SpanFelt252Serde of Serde> { fn serialize(self: @Span, ref output: Array) { (*self).len().serialize(ref output); From 1b65ad41ef0b47b68482a19c19d1e1fcb6cc2a27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Wed, 29 May 2024 17:47:49 +0200 Subject: [PATCH 05/10] test: add tests for Into impls on array --- corelib/src/test/array_test.cairo | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/corelib/src/test/array_test.cairo b/corelib/src/test/array_test.cairo index b9b8397295c..ade67478868 100644 --- a/corelib/src/test/array_test.cairo +++ b/corelib/src/test/array_test.cairo @@ -198,3 +198,43 @@ fn test_array_iterator() { i += 1; } } + +mod into_trait_impls { + #[test] + fn array_into_span() { + let array = array![1, 2, 3]; + + let span: Span<_> = array.into(); + + assert_eq!(span, array![1, 2, 3].span()) + } + + + #[test] + fn span_into_array() { + let span = array![1, 2, 3].span(); + + let array: Array<_> = span.into(); + + assert_eq!(array, array![1, 2, 3]); + } + + + #[test] + fn array_snap_into_span() { + let array_snap = @array![1, 2, 3]; + + let span: Span<_> = array_snap.into(); + + assert_eq!(span, array![1, 2, 3].span()) + } + + #[test] + fn span_into_array_snap() { + let span = array![1, 2, 3].span(); + + let array_snap: @Array<_> = span.into(); + + assert_eq!(array_snap, @array![1, 2, 3]); + } +} From 27b9dbd3406c84f288d5220717db7d5ce9502cf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Thu, 30 May 2024 10:55:17 +0200 Subject: [PATCH 06/10] tests fixes --- corelib/src/array.cairo | 2 +- corelib/src/test/array_test.cairo | 49 +++++++++---------------------- 2 files changed, 15 insertions(+), 36 deletions(-) diff --git a/corelib/src/array.cairo b/corelib/src/array.cairo index 2262071f28a..f4e8a6561d1 100644 --- a/corelib/src/array.cairo +++ b/corelib/src/array.cairo @@ -154,7 +154,7 @@ impl ArrayIntoSpan> of Into, Span> { impl ArraySnapIntoSpan of Into<@Array, Span> { fn into(self: @Array) -> Span { - self.span() + ArrayTrait::span(self) } } diff --git a/corelib/src/test/array_test.cairo b/corelib/src/test/array_test.cairo index ade67478868..9913589f3c9 100644 --- a/corelib/src/test/array_test.cairo +++ b/corelib/src/test/array_test.cairo @@ -199,42 +199,21 @@ fn test_array_iterator() { } } -mod into_trait_impls { - #[test] - fn array_into_span() { - let array = array![1, 2, 3]; - - let span: Span<_> = array.into(); - - assert_eq!(span, array![1, 2, 3].span()) - } - - - #[test] - fn span_into_array() { - let span = array![1, 2, 3].span(); - - let array: Array<_> = span.into(); - - assert_eq!(array, array![1, 2, 3]); - } - - - #[test] - fn array_snap_into_span() { - let array_snap = @array![1, 2, 3]; - - let span: Span<_> = array_snap.into(); - - assert_eq!(span, array![1, 2, 3].span()) - } +fn tets_array_into_span() { + assert_eq!(array![1, 2, 3].span(), array![1, 2, 3].into()) +} - #[test] - fn span_into_array_snap() { - let span = array![1, 2, 3].span(); +#[test] +fn tets_span_into_array() { + assert_eq!(array![1, 2, 3], array![1, 2, 3].span().into()); +} - let array_snap: @Array<_> = span.into(); +#[test] +fn tets_array_snap_into_span() { + assert_eq!(array![1, 2, 3].span(), (@array![1, 2, 3]).into()); +} - assert_eq!(array_snap, @array![1, 2, 3]); - } +#[test] +fn tets_span_into_array_snap() { + assert_eq!(@array![1, 2, 3], array![1, 2, 3].span().into()); } From a53c118f01ed6d0bf7f6fb893e683ef8346c3391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Thu, 30 May 2024 11:17:20 +0200 Subject: [PATCH 07/10] add generic impl of @C into Span based ond ArrayToSpan --- corelib/src/array.cairo | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/corelib/src/array.cairo b/corelib/src/array.cairo index f4e8a6561d1..8a6b4a07cd2 100644 --- a/corelib/src/array.cairo +++ b/corelib/src/array.cairo @@ -152,12 +152,6 @@ impl ArrayIntoSpan> of Into, Span> { } } -impl ArraySnapIntoSpan of Into<@Array, Span> { - fn into(self: @Array) -> Span { - ArrayTrait::span(self) - } -} - impl SpanIntoArray, +Clone> of Into, Array> { fn into(mut self: Span) -> Array { let mut arr = array![]; @@ -277,6 +271,12 @@ impl ArrayToSpan of ToSpanTrait, T> { } } +impl SnapIntoSpanWhereToSpanTrait> of Into<@C, Span> { + fn into(self: @C) -> Span { + self.span() + } +} + /// Returns a span from a box of struct of members of the same type. /// The additional `+Copy<@T>` arg is to prevent later stages from propagating the `S` type Sierra /// level, where it is deduced by the `T` type. From f63aee7e7dfb35bb0571d1ebf2558383069a9791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Wed, 5 Jun 2024 10:34:40 +0200 Subject: [PATCH 08/10] chore: typos in tests names --- corelib/src/test/array_test.cairo | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/corelib/src/test/array_test.cairo b/corelib/src/test/array_test.cairo index 9913589f3c9..44138ac2301 100644 --- a/corelib/src/test/array_test.cairo +++ b/corelib/src/test/array_test.cairo @@ -199,21 +199,21 @@ fn test_array_iterator() { } } -fn tets_array_into_span() { +fn test_array_into_span() { assert_eq!(array![1, 2, 3].span(), array![1, 2, 3].into()) } #[test] -fn tets_span_into_array() { +fn test_span_into_array() { assert_eq!(array![1, 2, 3], array![1, 2, 3].span().into()); } #[test] -fn tets_array_snap_into_span() { +fn test_array_snap_into_span() { assert_eq!(array![1, 2, 3].span(), (@array![1, 2, 3]).into()); } #[test] -fn tets_span_into_array_snap() { +fn test_span_into_array_snap() { assert_eq!(@array![1, 2, 3], array![1, 2, 3].span().into()); } From e9cf75051f1ae8615ae92a15c6a953339e025229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Wed, 5 Jun 2024 14:34:42 +0200 Subject: [PATCH 09/10] fix: remove unecessary mut --- corelib/src/array.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/corelib/src/array.cairo b/corelib/src/array.cairo index 8a6b4a07cd2..9b86ae84a3c 100644 --- a/corelib/src/array.cairo +++ b/corelib/src/array.cairo @@ -153,7 +153,7 @@ impl ArrayIntoSpan> of Into, Span> { } impl SpanIntoArray, +Clone> of Into, Array> { - fn into(mut self: Span) -> Array { + fn into(self: Span) -> Array { let mut arr = array![]; arr.append_span(self); arr From 41308d6650d9257a6233d458ae9a0afdfd02e552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Delabrouille?= Date: Thu, 6 Jun 2024 10:46:21 +0200 Subject: [PATCH 10/10] fmt --- corelib/src/array.cairo | 2 +- corelib/src/test/array_test.cairo | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/corelib/src/array.cairo b/corelib/src/array.cairo index 9b86ae84a3c..f020d7260ba 100644 --- a/corelib/src/array.cairo +++ b/corelib/src/array.cairo @@ -275,7 +275,7 @@ impl SnapIntoSpanWhereToSpanTrait> of Into<@C, Span> fn into(self: @C) -> Span { self.span() } -} +} /// Returns a span from a box of struct of members of the same type. /// The additional `+Copy<@T>` arg is to prevent later stages from propagating the `S` type Sierra diff --git a/corelib/src/test/array_test.cairo b/corelib/src/test/array_test.cairo index 44138ac2301..ad52919306a 100644 --- a/corelib/src/test/array_test.cairo +++ b/corelib/src/test/array_test.cairo @@ -205,7 +205,7 @@ fn test_array_into_span() { #[test] fn test_span_into_array() { - assert_eq!(array![1, 2, 3], array![1, 2, 3].span().into()); + assert_eq!(array![1, 2, 3], array![1, 2, 3].span().into()); } #[test] @@ -215,5 +215,5 @@ fn test_array_snap_into_span() { #[test] fn test_span_into_array_snap() { - assert_eq!(@array![1, 2, 3], array![1, 2, 3].span().into()); + assert_eq!(@array![1, 2, 3], array![1, 2, 3].span().into()); }