feat: add display UI with splash screen, dashboard, and RSSI sparkline
Implement full SSD1306 OLED display support: - Splash screen on boot (1.5s) with project name, version, board - Dashboard showing radio state, config, counters, RSSI/SNR - RSSI history sparkline (64-sample ring buffer, 2px-wide bars) - DisplayOn/DisplayOff host commands routed via dedicated channel - Pure render functions against generic DrawTarget (no hardware coupling) - Display on/off control bypasses radio task entirely Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
17624 files changed
+4564928
-38
No files matched your search
+1
@@ -0,0 +1 @@
|
||||
{"files":{".cargo_vcs_info.json":"38dee60f1390127d1f867d624108d7587bf2467ede7546760f18d67f3ab2986b",".github/bors.toml":"14c0045c0c38aa2d53d96827a7d1bf3428b48adff771cf9942be2ba449830afe",".github/workflows/clippy.yml":"a891fa341615a72cac12441f48b4a940e24e3f6497f95637de7054cb43f75414",".github/workflows/rustfmt.yml":"b6ebe7522813ba65b86792f632998da4464a40ca1625e15f69227319b64eb1fc",".github/workflows/test.yml":"6e87a809b6fcf7f8f3490eed69b84d6333e43145cfc80e23a5433ffb92bbd9a9","CHANGELOG.md":"2d2abc0d4599239c1b7bcb096b78288b292c5ddf9b4bcc48a475427707665a05","Cargo.lock":"12fb900ce4ed6edced0d375d640eb59b58f08564953b9e041ebc6ca6a26691f6","Cargo.toml":"f55978f3f888f411b49f5bbe9cc1c24f8bdb3603d99cdae3c806ae9ad7061051","Cargo.toml.orig":"c76f52bad74ef6909547275757e2e90ea7edab26969ab01abdfd84bd19ef6ce2","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"fee6bdac4b917351332567b9fd5013c2ef3c847dd113fd6216cf28e96637a157","README.md":"c0b9bf15b4566eaf16ae26dbb35169143d5bb09e2512cd929c5840e154aa6937","src/lib.rs":"79a2181f772357df213fbe64cde0300096b12e199a61e3308a4c50c4bdd76359"},"package":"0530892bb4fa575ee0da4b86f86c667132a94b74bb72160f58ee5a4afec74c23"}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"git": {
|
||||
"sha1": "5f57d792d4d0d1a92cb686b39d3e791a6177b8ac"
|
||||
},
|
||||
"path_in_vcs": ""
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
block_labels = ["needs-decision"]
|
||||
delete_merged_branches = true
|
||||
status = [
|
||||
"clippy (1.56)",
|
||||
"clippy (1.63)",
|
||||
"rustfmt",
|
||||
"test (1.56)",
|
||||
"test (1.63)",
|
||||
]
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
name: Clippy check
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ staging, trying, master ]
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
clippy:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- rust: 1.56
|
||||
- rust: 1.63
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Install Rust
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: ${{matrix.rust}}
|
||||
components: clippy
|
||||
override: true
|
||||
- name: Clippy check
|
||||
run: cargo clippy
|
||||
@@ -0,0 +1,14 @@
|
||||
name: Code formatting check
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ staging, trying, master ]
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
rustfmt:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Check fmt
|
||||
run: cargo fmt -- --check
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
name: Test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ staging, trying, master ]
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- rust: 1.56
|
||||
- rust: 1.63
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Install Rust
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: ${{matrix.rust}}
|
||||
components: clippy
|
||||
override: true
|
||||
- name: Test
|
||||
run: cargo test
|
||||
test-nightly:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Install Rust
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: nightly
|
||||
override: true
|
||||
- name: Test
|
||||
run: cargo test --features nightly
|
||||
Vendored
+40
@@ -0,0 +1,40 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## 2.1.1 - 2025-06-22
|
||||
|
||||
- Soundness fix: ConstStaticCell should only be Send/Sync if T: Send. (#19, #20)
|
||||
|
||||
## 2.1.0 - 2024-04-26 - YANKED
|
||||
|
||||
- Added `ConstStaticCell` (#14)
|
||||
|
||||
## 2.0.0 - 2023-11-02
|
||||
|
||||
- Do not enable feature `critical-section` in `portable-atomic`. In targets that need
|
||||
atomic polyfilling, you now need to explicitly enable either of `critical-section` or
|
||||
`unsafe-assume-single-core` in `portable-atomic`.
|
||||
|
||||
## 1.3.0 - 2023-11-02
|
||||
|
||||
- Switch from `atomic-polyfill` to `portable-atomic`. Note: to be backwards compatible
|
||||
this crate is enabling the `critical-section` feature in `portable-atomic`. If your
|
||||
chip is single-core, you might want to upgrade to `static-cell` v2.0 so you can enable
|
||||
the feature `unsafe-assume-single-core` instead, which is slightly more efficient.
|
||||
|
||||
## 1.2.0 - 2023-07-03
|
||||
|
||||
- Add panic-free `try_init()`, `try_init_with()`, `try_uninit()`.
|
||||
|
||||
## 1.1.0 - 2023-06-01
|
||||
|
||||
- Add `uninit()`.
|
||||
- Add `make_static!` macro.
|
||||
|
||||
## 1.0.0 - 2022-08-22
|
||||
|
||||
- First release
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic"
|
||||
version = "1.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483"
|
||||
|
||||
[[package]]
|
||||
name = "static_cell"
|
||||
version = "2.1.1"
|
||||
dependencies = [
|
||||
"portable-atomic",
|
||||
]
|
||||
Vendored
+42
@@ -0,0 +1,42 @@
|
||||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g., crates.io) dependencies.
|
||||
#
|
||||
# If you are reading this file be aware that the original Cargo.toml
|
||||
# will likely look very different (and much more reasonable).
|
||||
# See Cargo.toml.orig for the original contents.
|
||||
|
||||
[package]
|
||||
edition = "2021"
|
||||
name = "static_cell"
|
||||
version = "2.1.1"
|
||||
build = false
|
||||
autolib = false
|
||||
autobins = false
|
||||
autoexamples = false
|
||||
autotests = false
|
||||
autobenches = false
|
||||
description = "Statically allocated, initialized at runtime cell."
|
||||
readme = "README.md"
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/embassy-rs/static-cell"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
features = ["nightly"]
|
||||
rustdoc-args = [
|
||||
"--cfg",
|
||||
"docsrs",
|
||||
]
|
||||
|
||||
[features]
|
||||
nightly = []
|
||||
|
||||
[lib]
|
||||
name = "static_cell"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies.portable-atomic]
|
||||
version = "1.5.1"
|
||||
Vendored
+201
@@ -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 [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
Copyright (c) 2022 The critical-section authors
|
||||
|
||||
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.
|
||||
Vendored
+63
@@ -0,0 +1,63 @@
|
||||
# static-cell
|
||||
[](https://crates.io/crates/static_cell)
|
||||
[](https://crates.io/crates/static_cell)
|
||||
[](https://docs.rs/static_cell)
|
||||
|
||||
Statically allocated, initialized at runtime cell.
|
||||
|
||||
`StaticCell` provides a no-std-compatible, no-alloc way to reserve memory at compile time for
|
||||
a value, but initialize it at runtime, and get a `'static` reference to it.
|
||||
|
||||
This is useful in the following scenarios:
|
||||
|
||||
- You need `&'static T`, but `T` can't be constructed in const context so you can't simply use a `static`.
|
||||
- You need `&'static mut T`, not just `&'static T`.
|
||||
|
||||
## Example
|
||||
|
||||
```rust
|
||||
use static_cell::StaticCell;
|
||||
|
||||
// Statically allocate memory for a `u32`.
|
||||
static SOME_INT: StaticCell<u32> = StaticCell::new();
|
||||
|
||||
// Initialize it at runtime. This returns a `&'static mut`.
|
||||
let x: &'static mut u32 = SOME_INT.init(42);
|
||||
assert_eq!(*x, 42);
|
||||
|
||||
// Trying to call `.init()` again would panic, because the StaticCell is already initialized.
|
||||
// SOME_INT.init(42);
|
||||
```
|
||||
|
||||
## Alternatives
|
||||
|
||||
- If you can use `alloc`, you can use `Box::leak()`.
|
||||
- If you're OK with `unsafe`, you can use `static mut THING: MaybeUninit<T>`.
|
||||
- If you need just `&'static T` (instead of `&'static mut T`), there's [`OnceCell`](https://doc.rust-lang.org/stable/core/cell/struct.OnceCell.html) (not thread-safe though) or [`OnceLock`](https://doc.rust-lang.org/stable/std/sync/struct.OnceLock.html) (thread-safe, but requires `std`).
|
||||
|
||||
## Interoperability
|
||||
|
||||
This crate uses [`portable-atomic`](https://crates.io/crates/portable-atomic), so on targets without native
|
||||
atomics you must import a crate that provides a [`critical-section`](https://github.com/rust-embedded/critical-section)
|
||||
implementation. See the `critical-section` README for details.
|
||||
|
||||
## Minimum Supported Rust Version (MSRV)
|
||||
|
||||
This crate is guaranteed to compile on stable Rust 1.56 and up. It might compile with
|
||||
older versions but that may change in any new patch release.
|
||||
|
||||
## License
|
||||
|
||||
This work is licensed under either of
|
||||
|
||||
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
|
||||
<http://www.apache.org/licenses/LICENSE-2.0>)
|
||||
- MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
|
||||
|
||||
at your option.
|
||||
|
||||
## Contribution
|
||||
|
||||
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
|
||||
dual licensed as above, without any additional terms or conditions.
|
||||
Vendored
+307
@@ -0,0 +1,307 @@
|
||||
#![cfg_attr(not(test), no_std)]
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
#![cfg_attr(feature = "nightly", feature(type_alias_impl_trait))] // for tests
|
||||
|
||||
use core::cell::UnsafeCell;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
use portable_atomic::{AtomicBool, Ordering};
|
||||
|
||||
/// Statically allocated, initialized at runtime cell.
|
||||
///
|
||||
/// It has two states: "empty" and "full". It is created "empty", and obtaining a reference
|
||||
/// to the contents permanently changes it to "full". This allows that reference to be valid
|
||||
/// forever.
|
||||
///
|
||||
/// If your value can be initialized as a `const` value, consider using [`ConstStaticCell`]
|
||||
/// instead if you only need to take the value at runtime.
|
||||
///
|
||||
/// See the [crate-level docs](crate) for usage.
|
||||
pub struct StaticCell<T> {
|
||||
used: AtomicBool,
|
||||
val: UnsafeCell<MaybeUninit<T>>,
|
||||
}
|
||||
|
||||
unsafe impl<T> Send for StaticCell<T> {}
|
||||
unsafe impl<T> Sync for StaticCell<T> {}
|
||||
|
||||
impl<T> StaticCell<T> {
|
||||
/// Create a new, empty `StaticCell`.
|
||||
///
|
||||
/// It can be initialized at runtime with [`StaticCell::init()`] or similar methods.
|
||||
#[inline]
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
used: AtomicBool::new(false),
|
||||
val: UnsafeCell::new(MaybeUninit::uninit()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize the `StaticCell` with a value, returning a mutable reference to it.
|
||||
///
|
||||
/// Using this method, the compiler usually constructs `val` in the stack and then moves
|
||||
/// it into the `StaticCell`. If `T` is big, this is likely to cause stack overflows.
|
||||
/// Considering using [`StaticCell::init_with`] instead, which will construct it in-place inside the `StaticCell`.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this `StaticCell` is already full.
|
||||
#[inline]
|
||||
#[allow(clippy::mut_from_ref)]
|
||||
pub fn init(&'static self, val: T) -> &'static mut T {
|
||||
self.uninit().write(val)
|
||||
}
|
||||
|
||||
/// Initialize the `StaticCell` with the closure's return value, returning a mutable reference to it.
|
||||
///
|
||||
/// The advantage over [`StaticCell::init`] is that this method allows the closure to construct
|
||||
/// the `T` value in-place directly inside the `StaticCell`, saving stack space.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this `StaticCell` is already full.
|
||||
#[inline]
|
||||
#[allow(clippy::mut_from_ref)]
|
||||
pub fn init_with(&'static self, val: impl FnOnce() -> T) -> &'static mut T {
|
||||
self.uninit().write(val())
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the uninitialized memory owned by the `StaticCell`.
|
||||
///
|
||||
/// Using this method directly is not recommended, but it can be used to construct `T` in-place directly
|
||||
/// in a guaranteed fashion.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this `StaticCell` is already full.
|
||||
#[inline]
|
||||
#[allow(clippy::mut_from_ref)]
|
||||
pub fn uninit(&'static self) -> &'static mut MaybeUninit<T> {
|
||||
if let Some(val) = self.try_uninit() {
|
||||
val
|
||||
} else {
|
||||
panic!("`StaticCell` is already full, it can't be initialized twice.");
|
||||
}
|
||||
}
|
||||
|
||||
/// Try initializing the `StaticCell` with a value, returning a mutable reference to it.
|
||||
///
|
||||
/// If this `StaticCell` is already full, it returns `None`.
|
||||
///
|
||||
/// Using this method, the compiler usually constructs `val` in the stack and then moves
|
||||
/// it into the `StaticCell`. If `T` is big, this is likely to cause stack overflows.
|
||||
/// Considering using [`StaticCell::try_init_with`] instead, which will construct it in-place inside the `StaticCell`.
|
||||
///
|
||||
/// Will only return a Some(&'static mut T) when the `StaticCell` was not yet initialized.
|
||||
#[inline]
|
||||
#[allow(clippy::mut_from_ref)]
|
||||
pub fn try_init(&'static self, val: T) -> Option<&'static mut T> {
|
||||
Some(self.try_uninit()?.write(val))
|
||||
}
|
||||
|
||||
/// Try initializing the `StaticCell` with the closure's return value, returning a mutable reference to it.
|
||||
///
|
||||
/// If this `StaticCell` is already full, it returns `None`.
|
||||
///
|
||||
/// The advantage over [`StaticCell::init`] is that this method allows the closure to construct
|
||||
/// the `T` value in-place directly inside the `StaticCell`, saving stack space.
|
||||
///
|
||||
#[inline]
|
||||
#[allow(clippy::mut_from_ref)]
|
||||
pub fn try_init_with(&'static self, val: impl FnOnce() -> T) -> Option<&'static mut T> {
|
||||
Some(self.try_uninit()?.write(val()))
|
||||
}
|
||||
|
||||
/// Try returning a mutable reference to the uninitialized memory owned by the `StaticCell`.
|
||||
///
|
||||
/// If this `StaticCell` is already full, it returns `None`.
|
||||
///
|
||||
/// Using this method directly is not recommended, but it can be used to construct `T` in-place directly
|
||||
/// in a guaranteed fashion.
|
||||
#[inline]
|
||||
pub fn try_uninit(&'static self) -> Option<&'static mut MaybeUninit<T>> {
|
||||
if self
|
||||
.used
|
||||
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
|
||||
.is_ok()
|
||||
{
|
||||
// SAFETY: We just checked that the value is not yet taken and marked it as taken.
|
||||
let val = unsafe { &mut *self.val.get() };
|
||||
Some(val)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---
|
||||
|
||||
/// Statically allocated and initialized, taken at runtime cell.
|
||||
///
|
||||
/// It has two states: "untaken" and "taken". It is created "untaken", and obtaining a reference
|
||||
/// to the contents permanently changes it to "taken". This allows that reference to be valid
|
||||
/// forever.
|
||||
///
|
||||
/// If your value can be const defined, for example a large, zero filled buffer used for DMA
|
||||
/// or other scratch memory usage, `ConstStaticCell` can be used to guarantee the initializer
|
||||
/// will never take up stack memory.
|
||||
///
|
||||
/// If your values are all zero initialized, the resulting `ConstStaticCell` should be placed
|
||||
/// in `.bss`, not taking flash space for initialization either.
|
||||
///
|
||||
/// See the [crate-level docs](crate) for usage.
|
||||
pub struct ConstStaticCell<T> {
|
||||
taken: AtomicBool,
|
||||
val: UnsafeCell<T>,
|
||||
}
|
||||
|
||||
// ConstStaticCell<T> essentially lets you construct a value of T, pass &ConstStaticCell<T> to
|
||||
// another thread, and then in that thread obtain a &'static mut T, which is effectively sending
|
||||
// the value between threads, therefore T must implement Send.
|
||||
unsafe impl<T: Send> Send for ConstStaticCell<T> {}
|
||||
unsafe impl<T: Send> Sync for ConstStaticCell<T> {}
|
||||
|
||||
impl<T> ConstStaticCell<T> {
|
||||
/// Create a new, empty `ConstStaticCell`.
|
||||
///
|
||||
/// It can be taken at runtime with [`ConstStaticCell::take()`] or similar methods.
|
||||
#[inline]
|
||||
pub const fn new(value: T) -> Self {
|
||||
Self {
|
||||
taken: AtomicBool::new(false),
|
||||
val: UnsafeCell::new(value),
|
||||
}
|
||||
}
|
||||
|
||||
/// Take the `ConstStaticCell`, returning a mutable reference to it.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if this `ConstStaticCell` was already taken.
|
||||
#[inline]
|
||||
#[allow(clippy::mut_from_ref)]
|
||||
pub fn take(&'static self) -> &'static mut T {
|
||||
if let Some(val) = self.try_take() {
|
||||
val
|
||||
} else {
|
||||
panic!("`ConstStaticCell` is already taken, it can't be taken twice")
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to take the `ConstStaticCell`, returning None if it was already taken
|
||||
#[inline]
|
||||
#[allow(clippy::mut_from_ref)]
|
||||
pub fn try_take(&'static self) -> Option<&'static mut T> {
|
||||
if self
|
||||
.taken
|
||||
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
|
||||
.is_ok()
|
||||
{
|
||||
// SAFETY: We just checked that the value is not yet taken and marked it as taken.
|
||||
let val = unsafe { &mut *self.val.get() };
|
||||
Some(val)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a `T` to a `&'static mut T`.
|
||||
///
|
||||
/// The macro declares a `static StaticCell` and then initializes it when run, returning the `&'static mut`.
|
||||
/// Therefore, each instance can only be run once. Next runs will panic. The `static` can additionally be
|
||||
/// decorated with attributes, such as `#[link_section]`, `#[used]`, et al.
|
||||
///
|
||||
/// This macro is nightly-only. It requires `#![feature(type_alias_impl_trait)]` in the crate using it.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # #![feature(type_alias_impl_trait)]
|
||||
/// use static_cell::make_static;
|
||||
///
|
||||
/// # fn main() {
|
||||
/// let x: &'static mut u32 = make_static!(42);
|
||||
///
|
||||
/// // This attribute instructs the linker to allocate it in the external RAM's BSS segment.
|
||||
/// // This specific example is for ESP32S3 with PSRAM support.
|
||||
/// let buf = make_static!([0u8; 4096], #[link_section = ".ext_ram.bss.buf"]);
|
||||
///
|
||||
/// // Multiple attributes can be supplied.
|
||||
/// let s = make_static!(0usize, #[used] #[export_name = "exported_symbol_name"]);
|
||||
/// # }
|
||||
/// ```
|
||||
#[cfg(feature = "nightly")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
|
||||
#[macro_export]
|
||||
macro_rules! make_static {
|
||||
($val:expr) => ($crate::make_static!($val, ));
|
||||
($val:expr, $(#[$m:meta])*) => {{
|
||||
type T = impl ::core::marker::Sized;
|
||||
$(#[$m])*
|
||||
static STATIC_CELL: $crate::StaticCell<T> = $crate::StaticCell::new();
|
||||
#[deny(unused_attributes)]
|
||||
let (x,) = STATIC_CELL.uninit().write(($val,));
|
||||
x
|
||||
}};
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::StaticCell;
|
||||
|
||||
#[test]
|
||||
fn test_static_cell() {
|
||||
static CELL: StaticCell<u32> = StaticCell::new();
|
||||
let val: &'static u32 = CELL.init(42u32);
|
||||
assert_eq!(*val, 42);
|
||||
}
|
||||
|
||||
#[cfg(feature = "nightly")]
|
||||
#[test]
|
||||
fn test_make_static() {
|
||||
let val: &'static u32 = make_static!(42u32);
|
||||
assert_eq!(*val, 42);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(doctest)]
|
||||
mod compile_fail_tests {
|
||||
/// ```compile_fail
|
||||
/// use std::rc::Rc;
|
||||
///
|
||||
/// use static_cell::ConstStaticCell;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let rc = Rc::new(0);
|
||||
/// let sc = ConstStaticCell::new(rc.clone());
|
||||
/// let t = std::thread::spawn(move || {
|
||||
/// let sc = Box::leak(Box::new(sc));
|
||||
/// let rc: &mut Rc<i32> = sc.take();
|
||||
/// _ = rc.clone(); // data race!
|
||||
/// });
|
||||
/// _ = rc.clone();
|
||||
/// _ = t.join();
|
||||
/// }
|
||||
/// ```
|
||||
fn send_requires_send() {}
|
||||
|
||||
/// ```compile_fail
|
||||
/// use std::rc::Rc;
|
||||
///
|
||||
/// use static_cell::ConstStaticCell;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let rc = Rc::new(0);
|
||||
/// let sc = Box::leak(Box::new(ConstStaticCell::new(rc.clone())));
|
||||
/// let t = std::thread::spawn(|| {
|
||||
/// let rc: &mut Rc<i32> = sc.take();
|
||||
/// _ = rc.clone(); // data race!
|
||||
/// });
|
||||
/// _ = rc.clone();
|
||||
/// _ = t.join();
|
||||
/// }
|
||||
/// ```
|
||||
fn sync_requires_send() {}
|
||||
}
|
||||
Reference in new issue
Block a user