From e60e049a7fa4485b1f29ae1a3d5855f8ca6fc355 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Sun, 18 Aug 2024 10:41:38 -0700 Subject: [PATCH] Readme + clippy + Linear refactor --- .rustme/config.ron | 65 +++++++++++++++ .rustme/docs.md | 49 +++++++++++ .rustme/header.md | 1 + CODE_OF_CONDUCT.md | 128 +++++++++++++++++++++++++++++ CONTRIBUTING.md | 55 +++++++++++++ Cargo.toml | 10 +++ LICENSE-APACHE | 201 +++++++++++++++++++++++++++++++++++++++++++++ LICENSE-MIT | 8 ++ README.md | 59 +++++++++++++ src/.crate-docs.md | 57 +++++++++++++ src/easings.rs | 108 +++++++++++------------- src/lib.rs | 32 ++------ 12 files changed, 686 insertions(+), 87 deletions(-) create mode 100644 .rustme/config.ron create mode 100644 .rustme/docs.md create mode 100644 .rustme/header.md create mode 100644 CODE_OF_CONDUCT.md create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE-APACHE create mode 100644 LICENSE-MIT create mode 100644 README.md create mode 100644 src/.crate-docs.md diff --git a/.rustme/config.ron b/.rustme/config.ron new file mode 100644 index 0000000..4f87d38 --- /dev/null +++ b/.rustme/config.ron @@ -0,0 +1,65 @@ +Configuration( + files: { + "../README.md": ( + for_docs: false, + sections: [ + "header.md", + "docs.md", + "https://github.com/khonsulabs/.github/raw/main/snippets/readme-footer.md", + ], + ), + "../src/.crate-docs.md": ( + for_docs: true, + sections: [ + "docs.md", + "https://github.com/khonsulabs/.github/raw/main/snippets/readme-footer.md", + ], + ), + "../CONTRIBUTING.md": [ + "https://github.com/khonsulabs/.github/raw/main/docs/CONTRIBUTING.md", + ], + "../CODE_OF_CONDUCT.md": [ + "https://github.com/khonsulabs/.github/raw/main/docs/CODE_OF_CONDUCT.md", + ], + "../LICENSE-APACHE": [ + "https://github.com/khonsulabs/.github/raw/main/licenses/LICENSE-APACHE", + ], + "../LICENSE-MIT": [ + "https://github.com/khonsulabs/.github/raw/main/licenses/LICENSE-MIT", + ], + }, + glossaries: [ + "https://github.com/khonsulabs/.github/raw/main/snippets/glossary.ron", + { + "msrv": "1.65", + "ref-name": ( + default: "main", + release: "v0.1.0", + ), + "docs": ( + default: "https://khonsulabs.github.io/easing-function/main/easing_function/", + release: "https://docs.rs/easing-function/", + ), + "easing": ( + default: "https://khonsulabs.github.io/easing-function/main/easing_function/trait.Easing.html", + release: "https://docs.rs/easing-function/*/easing_function/trait.Easing.html", + for_docs: "crate::Easing", + ), + "easingfunction": ( + default: "https://khonsulabs.github.io/easing-function/main/easing_function/struct.EasingFunction.html", + release: "https://docs.rs/easing-function/*/easing_function/struct.EasingFunction.html", + for_docs: "crate::EasingFunction", + ), + "standard": ( + default: "https://khonsulabs.github.io/easing-function/main/easing_function/easings/enum.StandardEasing.html", + release: "https://docs.rs/easing-function/*/easing_function/easings/enum.StandardEasing.html", + for_docs: "crate::easings::StandardEasing", + ), + "sine": ( + default: "https://khonsulabs.github.io/easing-function/main/easing_function/easings/struct.EaseInOutSine.html", + release: "https://docs.rs/easing-function/*/easing_function/easings/struct.EaseInOutSine.html", + for_docs: "crate::easings::EaseInOutSine", + ), + }, + ], +) \ No newline at end of file diff --git a/.rustme/docs.md b/.rustme/docs.md new file mode 100644 index 0000000..6ce4c4f --- /dev/null +++ b/.rustme/docs.md @@ -0,0 +1,49 @@ + + + +![easing-function is considered alpha](https://img.shields.io/badge/status-alpha-orange) +[![crate version](https://img.shields.io/crates/v/muse.svg)](https://crates.io/crates/easing-function) +[![Documentation for `$ref-name$`](https://img.shields.io/badge/docs-$ref-name$-informational)]($docs$) + +A Rusty implementation of easing functions. + +## What is an Easing Function? + +An easing function is a function that describes how to apply a change of a value +over time. This library's easing functions expect input values between 0.0 and +1.0 and will return values corresponding to that range but may be outside of the +range depending on the function. + +Easing functions are typically used in animation systems to customize values +produced between keyframes (tweening). + +## Why another easing functions crate? + +When creating [Cushy](https://github.com/khonsulabs/cushy), no maintained +library seemed to offer a trait-based solution to allow for custom easing +functions in addition to the standard ones inspired by Robert Penner's original +collection. + +## Using this crate + +The [`EasingFunction`][easingfunction] type is the central type of this crate. It allows +defining an easing function in two ways: + +- [`EasingFunction::from_fn`][from_fn]: Creates an easing function from a `fn(f32) -> + f32` function. +- [`EasingFunction::new`][new]: Creates an easing function from an [`Easing`][easing] + implementor. + +This crate also provides the standard set of easing functions in the +[easings][easings] module (e.g., [`EaseInOutSine`][sine]). Finally, the +[`StandardEasings`][standard] enum provides access to the standard easing +functions through an enumeration. This enum also supports `serde` when enabling +the `serde` feature of this crate. + +[easing]: $easing$ +[easingfunction]: $easingfunction$ +[new]: $easingfunction$#method.new +[from_fn]: $easingfunction$#method.from_fn +[sine]: $sine$ +[standard]: $standard$ + diff --git a/.rustme/header.md b/.rustme/header.md new file mode 100644 index 0000000..74afd14 --- /dev/null +++ b/.rustme/header.md @@ -0,0 +1 @@ +# easing-function diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..f5e94d4 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,128 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +moderators@khonsulabs.com. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..56b08ab --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,55 @@ +# Contributing to our projects + +Thank you for your interest in contributing to one of our projects. We want +everyone to have a positive experience contributing, so please carefully review +our only requirements for contributing: + +- All contributors must agree to [our Contributor License + Agreement](https://gist.github.com/ecton/b2e1e72abfa122da5e69ed30164f739e). + This will be asked for during your first pull request. +- All contributors must uphold the standards of our [Code of + Conduct](./CODE_OF_CONDUCT.md). + +The rest of this document is recommendations/guidelines to help consistency and +communication within our projects. + +## Creating Issues + +### Reporting Bugs + +To us, if something isn't behaving as you expect it to, that's a bug. Even if +it's misbehaving due to a misunderstanding, that means there's an opportunity to +improve our documentation or examples. Please don't hesitate to let us know if +you run into any issues while working with one of our projects. + +### Requesting New Features + +When requesting new features, please include details about what problem you're +trying to solve, not just a solution to your problem. By helping the community +understand the underlying problem, we can better evaluate what the best solution +to the problem might be. + +## Contributing Changes + +We openly welcome pull requests on our projects. We don't like bugs, and if +you've found one and wish to submit a fix, we greatly appreciate it. + +If you find that fixing a bug requires a significant change, or you are wanting +to add a somewhat large feature, please submit a proposal as an issue first. We +want to make sure that your efforts have the highest chance of success, and a +short discussion before starting can go a long way towards a pull request being +merged with less revisions. + +When working on an existing issue, update the issue to reflect that you're +working on it. This will help prevent duplicated efforts. + +If you begin working on something but need some assistance, don't hesitate to +reach out inside of the issue, on [our +forums](https://community.khonsulabs.com/), or in [our +Discord](https://discord.khonsulabs.com/). We will do our best to help you. + +### Project-specific requirements + +Be sure to check if a project's README contains additional contributing +guidelines. Each project may have different tools and commands that should be +run to validate that changes pass all requirements. diff --git a/Cargo.toml b/Cargo.toml index 437320f..00c66aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,13 @@ serde = ["dep:serde"] [dependencies] serde = { version = "1.0.208", optional = true, features = ["derive"] } + +[lints.clippy] +pedantic = { level = "warn", priority = -1 } +module_name_repetitions = "allow" +missing_errors_doc = "allow" +missing_panics_doc = "allow" + +[lints.rust] +unsafe_code = "forbid" +missing_docs = "warn" diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..28add2d --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright 2021 Khonsu Labs LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..2b84906 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,8 @@ +Copyright 2021 Khonsu Labs LLC + +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..35d4615 --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# easing-function + + + + +![easing-function is considered alpha](https://img.shields.io/badge/status-alpha-orange) +[![crate version](https://img.shields.io/crates/v/muse.svg)](https://crates.io/crates/easing-function) +[![Documentation for `main`](https://img.shields.io/badge/docs-main-informational)](https://khonsulabs.github.io/easing-function/main/easing_function/) + +A Rusty implementation of easing functions. + +## What is an Easing Function? + +An easing function is a function that describes how to apply a change of a value +over time. This library's easing functions expect input values between 0.0 and +1.0 and will return values corresponding to that range but may be outside of the +range depending on the function. + +Easing functions are typically used in animation systems to customize values +produced between keyframes (tweening). + +## Why another easing functions crate? + +When creating [Cushy](https://github.com/khonsulabs/cushy), no maintained +library seemed to offer a trait-based solution to allow for custom easing +functions in addition to the standard ones inspired by Robert Penner's original +collection. + +## Using this crate + +The [`EasingFunction`][easingfunction] type is the central type of this crate. It allows +defining an easing function in two ways: + +- [`EasingFunction::from_fn`][from_fn]: Creates an easing function from a `fn(f32) -> + f32` function. +- [`EasingFunction::new`][new]: Creates an easing function from an [`Easing`][easing] + implementor. + +This crate also provides the standard set of easing functions in the +[easings][easings] module (e.g., [`EaseInOutSine`][sine]). Finally, the +[`StandardEasings`][standard] enum provides access to the standard easing +functions through an enumeration. This enum also supports `serde` when enabling +the `serde` feature of this crate. + +[easing]: https://khonsulabs.github.io/easing-function/main/easing_function/trait.Easing.html +[easingfunction]: https://khonsulabs.github.io/easing-function/main/easing_function/struct.EasingFunction.html +[new]: https://khonsulabs.github.io/easing-function/main/easing_function/struct.EasingFunction.html#method.new +[from_fn]: https://khonsulabs.github.io/easing-function/main/easing_function/struct.EasingFunction.html#method.from_fn +[sine]: https://khonsulabs.github.io/easing-function/main/easing_function/easings/struct.EaseInOutSine.html +[standard]: https://khonsulabs.github.io/easing-function/main/easing_function/easings/enum.StandardEasing.html + + +## Open-source Licenses + +This project, like all projects from [Khonsu Labs](https://khonsulabs.com/), is open-source. +This repository is available under the [MIT License](./LICENSE-MIT) or the +[Apache License 2.0](./LICENSE-APACHE). + +To learn more about contributing, please see [CONTRIBUTING.md](./CONTRIBUTING.md). diff --git a/src/.crate-docs.md b/src/.crate-docs.md new file mode 100644 index 0000000..90d1f0d --- /dev/null +++ b/src/.crate-docs.md @@ -0,0 +1,57 @@ + + + +![easing-function is considered alpha](https://img.shields.io/badge/status-alpha-orange) +[![crate version](https://img.shields.io/crates/v/muse.svg)](https://crates.io/crates/easing-function) +[![Documentation for `main`](https://img.shields.io/badge/docs-main-informational)](https://khonsulabs.github.io/easing-function/main/easing_function/) + +A Rusty implementation of easing functions. + +## What is an Easing Function? + +An easing function is a function that describes how to apply a change of a value +over time. This library's easing functions expect input values between 0.0 and +1.0 and will return values corresponding to that range but may be outside of the +range depending on the function. + +Easing functions are typically used in animation systems to customize values +produced between keyframes (tweening). + +## Why another easing functions crate? + +When creating [Cushy](https://github.com/khonsulabs/cushy), no maintained +library seemed to offer a trait-based solution to allow for custom easing +functions in addition to the standard ones inspired by Robert Penner's original +collection. + +## Using this crate + +The [`EasingFunction`][easingfunction] type is the central type of this crate. It allows +defining an easing function in two ways: + +- [`EasingFunction::from_fn`][from_fn]: Creates an easing function from a `fn(f32) -> + f32` function. +- [`EasingFunction::new`][new]: Creates an easing function from an [`Easing`][easing] + implementor. + +This crate also provides the standard set of easing functions in the +[easings][easings] module (e.g., [`EaseInOutSine`][sine]). Finally, the +[`StandardEasings`][standard] enum provides access to the standard easing +functions through an enumeration. This enum also supports `serde` when enabling +the `serde` feature of this crate. + +[easing]: crate::Easing +[easingfunction]: crate::EasingFunction +[new]: crate::EasingFunction#method.new +[from_fn]: crate::EasingFunction#method.from_fn +[sine]: crate::easings::EaseInOutSine +[standard]: crate::easings::StandardEasing + + +## Open-source Licenses + +This project, like all projects from [Khonsu Labs](https://khonsulabs.com/), is open-source. +This repository is available under the [MIT License](./LICENSE-MIT) or the +[Apache License 2.0](./LICENSE-APACHE). + +To learn more about contributing, please see [CONTRIBUTING.md](./CONTRIBUTING.md). diff --git a/src/easings.rs b/src/easings.rs index 52a1a29..5695407 100644 --- a/src/easings.rs +++ b/src/easings.rs @@ -4,40 +4,18 @@ use core::f32::consts::PI; use crate::{Easing, EasingFunction, EasingKind}; -/// An [`Easing`] function that produces a steady, linear transition. -#[derive(Clone, Copy, Debug)] -pub struct Linear; - -impl Linear { - pub fn ease(progress: f32) -> f32 { - progress - } -} - -impl Easing for Linear { - fn ease(&self, progress: f32) -> f32 { - progress - } -} - -impl From for EasingFunction { - fn from(_: Linear) -> Self { - Self::from_fn(Linear::ease) - } -} - macro_rules! declare_easing_function { - ($name:ident, $anchor_name:ident, $description:literal, $closure:expr) => { + ($name:ident, $($anchor_name:ident)?, $description:literal, $closure:expr) => { /// An [`Easing`] function that eases #[doc = $description] - #[doc = concat!(".\n\nSee for a visualization and more information.")] + $(#[doc = concat!("\n\nSee for a visualization and more information.")])? #[derive(Clone, Copy, Debug)] pub struct $name; impl $name { /// Eases #[doc = $description] - #[doc = concat!("\n\nSee for a visualization and more information.")] + $(#[doc = concat!("\n\nSee for a visualization and more information.")])? #[must_use] pub fn ease(progress: f32) -> f32 { let closure = force_closure_type($closure); @@ -60,21 +38,24 @@ macro_rules! declare_easing_function { } macro_rules! declare_easing_functions { - ($(($name:ident, $anchor_name:ident, $name_no_ease:ident, $description:literal, $closure:expr)),+) => { + ($(($name:ident, $([$anchor_name:ident],)? $name_no_ease:ident, $description:literal, $closure:expr)),+) => { + /// An enumeration of all strandard easings provided. #[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum StandardEasing { $( /// Eases #[doc = $description] - #[doc = concat!("\n\nSee for a visualization and more information.")] - $name_no_ease - ),+ + $(#[doc = concat!("\n\nSee for a visualization and more information.")])? + $name_no_ease, + )+ } impl StandardEasing { + /// Returns a collection of every variant of this enum. + #[must_use] pub fn all() -> &'static [StandardEasing] { - static ALL: [StandardEasing; 30] = [ + static ALL: [StandardEasing; 31] = [ $(StandardEasing::$name_no_ease),+ ]; &ALL @@ -118,11 +99,13 @@ macro_rules! declare_easing_functions { } $( - declare_easing_function!($name, $anchor_name, $description, $closure); + declare_easing_function!($name, $($anchor_name)?, $description, $closure); )+ }; } +/// An error returned from [`StandardEasing::try_from`] indicating the +/// [`EasingFunction`] is not a standard easing function. #[derive(Debug, Clone, PartialEq)] pub struct NonStandardEasing(pub EasingFunction); @@ -134,42 +117,42 @@ fn force_closure_type(f: impl Fn(f32) -> f32) -> impl Fn(f32) -> f32 { declare_easing_functions!( ( EaseInSine, - easeInSine, + [easeInSine], InSine, "in using a sine wave", |percent| 1. - (percent * PI / 2.).cos() ), ( EaseOutSine, - easeOutSine, + [easeOutSine], OutSine, "out using a sine wave", |percent| (percent * PI / 2.).sin() ), ( EaseInOutSine, - easeInOutSine, + [easeInOutSine], InOutSine, "in and out using a sine wave", |percent| -((percent * PI).cos() - 1.) / 2. ), ( EaseInQuadradic, - easeInQuad, + [easeInQuad], InQuadradic, "in using a quadradic (x^2) curve", squared ), ( EaseOutQuadradic, - easeOutQuad, + [easeOutQuad], OutQuadradic, "out using a quadradic (x^2) curve", |percent| 1. - squared(1. - percent) ), ( EaseInOutQuadradic, - easeInOutQuad, + [easeInOutQuad], InOutQuadradic, "in and out using a quadradic (x^2) curve", |percent| { @@ -182,21 +165,21 @@ declare_easing_functions!( ), ( EaseInCubic, - easeInCubic, + [easeInCubic], InCubic, "in using a cubic (x^3) curve", cubed ), ( EaseOutCubic, - easeOutCubic, + [easeOutCubic], OutCubic, "out using a cubic (x^3) curve", |percent| 1. - cubed(1. - percent) ), ( EaseInOutCubic, - easeInOutCubic, + [easeInOutCubic], InOutCubic, "in and out using a cubic (x^3) curve", |percent| { @@ -209,21 +192,21 @@ declare_easing_functions!( ), ( EaseInQuartic, - easeInQuart, + [easeInQuart], InQuartic, "in using a quartic (x^4) curve", quarted ), ( EaseOutQuartic, - easeOutQuart, + [easeOutQuart], OutQuartic, "out using a quartic (x^4) curve", |percent| 1. - quarted(1. - percent) ), ( EaseInOutQuartic, - easeInOutQuart, + [easeInOutQuart], InOutQuartic, "in and out using a quartic (x^4) curve", |percent| { @@ -236,21 +219,21 @@ declare_easing_functions!( ), ( EaseInQuintic, - easeInQuint, + [easeInQuint], InQuintic, "in using a quintic (x^5) curve", quinted ), ( EaseOutQuintic, - easeOutQuint, + [easeOutQuint], OutQuintic, "out using a quintic (x^5) curve", |percent| 1. - quinted(1. - percent) ), ( EaseInOutQuintic, - easeInOutQuint, + [easeInOutQuint], InOutQuintic, "in and out using a quintic (x^5) curve", |percent| { @@ -263,21 +246,21 @@ declare_easing_functions!( ), ( EaseInExponential, - easeInExpo, + [easeInExpo], InExponential, "in using an expenential curve", |percent| { 2f32.powf(10. * percent - 10.) } ), ( EaseOutExponential, - easeOutExpo, + [easeOutExpo], OutExponential, "out using an expenential curve", |percent| { 1. - 2f32.powf(-10. * percent) } ), ( EaseInOutExponential, - easeInOutExpo, + [easeInOutExpo], InOutExponential, "in and out using an expenential curve", |percent| if percent < 0.5 { @@ -288,21 +271,21 @@ declare_easing_functions!( ), ( EaseInCircular, - easeInCirc, + [easeInCirc], InCircular, "in using a curve resembling the top-left arc of a circle", |percent| 1. - (1. - squared(percent)).sqrt() ), ( EaseOutCircular, - easeOutCirc, + [easeOutCirc], OutCircular, "out using a curve resembling the top-left arc of a circle", |percent| (1. - squared(percent - 1.)).sqrt() ), ( EaseInOutCircular, - easeInOutCirc, + [easeInOutCirc], InOutCircular, "in and out using a curve resembling the top-left arc of a circle", |percent| { @@ -315,7 +298,7 @@ declare_easing_functions!( ), ( EaseInBack, - easeInBack, + [easeInBack], InBack, "in using a curve that backs away initially", |percent| { @@ -326,7 +309,7 @@ declare_easing_functions!( ), ( EaseOutBack, - easeOutBack, + [easeOutBack], OutBack, "out using a curve that backs away initially", |percent| { @@ -338,7 +321,7 @@ declare_easing_functions!( ), ( EaseInOutBack, - easeInOutBack, + [easeInOutBack], InOutBack, "in and out using a curve that backs away initially", |percent| { @@ -351,21 +334,21 @@ declare_easing_functions!( ), ( EaseInElastic, - easeInElastic, + [easeInElastic], InElastic, "in using a curve that bounces around the start initially then quickly accelerates", |percent| { -(2f32.powf(10. * percent - 10.)) * ((percent * 10. - 10.75) * C4).sin() } ), ( EaseOutElastic, - easeOutElastic, + [easeOutElastic], OutElastic, "out using a curve that bounces around the start initially then quickly accelerates", |percent| { 2f32.powf(-10. * percent) * ((percent * 10. - 0.75) * C4).sin() + 1. } ), ( EaseInOutElastic, - easeInOutElastic, + [easeInOutElastic], InOutElastic, "in and out using a curve that bounces around the start initially then quickly accelerates", |percent| if percent < 0.5 { @@ -376,14 +359,14 @@ declare_easing_functions!( ), ( EaseInBounce, - easeInBounce, + [easeInBounce], InBounce, "in using a curve that bounces progressively closer as it progresses", |percent| 1. - EaseOutBounce.ease(1. - percent) ), ( EaseOutBounce, - easeOutBounce, + [easeOutBounce], OutBounce, "out using a curve that bounces progressively closer as it progresses", |percent| { @@ -406,7 +389,7 @@ declare_easing_functions!( ), ( EaseInOutBounce, - easeInOutBounce, + [easeInOutBounce], InOutBounce, "in and out using a curve that bounces progressively closer as it progresses", |percent| { @@ -416,7 +399,8 @@ declare_easing_functions!( (1. + EaseOutBounce::ease(2. * percent - 1.)) / 2. } } - ) + ), + (Linear, Linear, "linearly", |percent| percent) ); fn squared(value: f32) -> f32 { diff --git a/src/lib.rs b/src/lib.rs index 78bed08..62beb6c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ +#![doc = include_str!(".crate-docs.md")] use std::fmt::Debug; +use std::sync::Arc; use easings::Linear; @@ -9,15 +11,17 @@ pub mod easings; pub struct EasingFunction(EasingKind); impl EasingFunction { + /// Returns a new easing function using `func`. pub fn from_fn(func: fn(f32) -> f32) -> Self { Self(EasingKind::Fn(func)) } + /// Returns a new easing function using `easing`. pub fn new(easing: Easing) -> Self where Easing: crate::Easing + Debug + Clone, { - Self(EasingKind::Custom(Box::new(easing))) + Self(EasingKind::Custom(Arc::new(easing))) } } @@ -33,12 +37,12 @@ impl Default for EasingFunction { } } -#[derive(Debug)] +#[derive(Debug, Clone)] enum EasingKind { /// A function pointer to use as an easing function. Fn(fn(f32) -> f32), /// A custom easing implementation. - Custom(Box), + Custom(Arc), } impl Easing for EasingKind { @@ -60,28 +64,6 @@ impl PartialEq for EasingKind { } } -impl Clone for EasingKind { - fn clone(&self) -> Self { - match self { - Self::Fn(func) => Self::Fn(*func), - Self::Custom(func) => Self::Custom(func.clone_boxed()), - } - } -} - -trait EasingClone: Debug + Easing { - fn clone_boxed(&self) -> Box; -} - -impl EasingClone for T -where - T: Debug + Clone + Easing, -{ - fn clone_boxed(&self) -> Box { - Box::new(self.clone()) - } -} - /// Performs easing for value interpolation. pub trait Easing: Debug + Send + Sync + 'static { /// Eases a value ranging between zero and one. The resulting value does not