swizzling for arrays (and tuples)
init
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Cargo.toml | 11 | ||||
| -rw-r--r-- | LICENSE | 21 | ||||
| -rw-r--r-- | README.md | 11 | ||||
| -rw-r--r-- | src/lib.rs | 35 |
5 files changed, 79 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03314f7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..cade0a6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "swizzle" +version = "0.1.0" +edition = "2024" +authors = ["bendn <[email protected]>"] +description = "swizzling for arrays (and tuples)" +repository = "https://github.com/bend-n/swizzle" +license = "MIT" + +[package.metadata.docs.rs] +rustdoc-args = ["--generate-link-to-definition"] @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 bendn + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..419056e --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# swizzle + +simd swizzle for arrays. + +```rust +use swizzle::array; +assert_eq!( + [10, 11, 12, 13, 14, 15].swizzle([3, 0, 1, 2, 5]), + [13, 10, 11, 12, 15] +); +``` diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..641402e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,35 @@ +#[allow(non_camel_case_types)] +/// Swizzle an array. The result is `a[index[i]]`. +pub trait array<T> { + /// Provides a method to arrays that selects specific indexes. + /// ``` + /// use swizzle::array; + /// assert_eq!( + /// [10, 11, 12, 13, 14, 15].swizzle([3, 0, 1, 2, 5]), + /// [13, 10, 11, 12, 15] + /// ); + /// ``` + /// The result is `self[indexes[i]]` + fn swizzle<const N: usize>(self, indexes: [usize; N]) -> [T; N]; +} + +impl<T: Clone, const N_: usize> array<T> for [T; N_] { + fn swizzle<const N: usize>(self, indexes: [usize; N]) -> [T; N] { + std::array::from_fn(|i| self[indexes[i]].clone()) + } +} + +#[macro_export] +/// Swizzling for tuples. I think arrays are better but i added this anyways. +/// ``` +/// assert_eq!( +/// swizzle::tuple!((10u8, 11i8, 12i32, 13u32, 14i64, 15u64), [3, 0, 1, 2, 5]), +/// (13, 10, 11, 12, 15), +/// ); +/// ``` +macro_rules! tuple { + ($x:expr, [$($index:tt),* $(,)?]) => {{ + let t = $x; + ($(t.$index,)*) + }}; +} |