Skip to content

Commit

Permalink
feat: redo initialize for impl + no_std + ci (#1)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun authored Oct 26, 2024
1 parent 4189862 commit 2676138
Show file tree
Hide file tree
Showing 17 changed files with 836 additions and 207 deletions.
19 changes: 19 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 tison <wander4096@gmail.com>
#
# 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.

[alias]
x = "run --package x --"

[env]
CARGO_WORKSPACE_DIR = { value = "", relative = true }
16 changes: 16 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2024 tison <wander4096@gmail.com>
#
# 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.

github: tisonkun

28 changes: 14 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,28 @@ concurrency:
jobs:
check:
name: Check
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: korandoru/hawkeye@v5
- name: Delete rust-toolchain.toml
run: rm rust-toolchain.toml
- name: Install toolchain
uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt,clippy
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@v2
with:
tool: typos-cli,taplo-cli
- name: Check Format
run: cargo fmt --all
- name: Check Clippy
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Check Toml
run: taplo format --check
- name: Check Typos
run: typos
tool: typos-cli,taplo-cli,hawkeye
- run: cargo x lint

test:
name: Run tests
runs-on: ubuntu-latest
strategy:
matrix:
os: [ ubuntu-22.04, macos-14, windows-2022 ]
rust-version: [ "1.75.0", "stable" ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
Expand All @@ -68,11 +67,12 @@ jobs:
with:
toolchain: ${{ matrix.rust-version }}
- name: Run unit tests
run: cargo test --all-features -- --nocapture
run: cargo x test --no-capture
shell: bash

required:
name: Required
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
if: ${{ always() }}
needs:
- check
Expand Down
20 changes: 5 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

[package]
name = "mea"
version = "0.0.1"
[workspace]
members = ["mea", "xtask"]
resolver = "2"

[workspace.package]
edition = "2021"
rust-version = "1.75.0"

description = "Async Rust utilities that are runtime agnostic."
documentation = "https://docs.rs/mea"
homepage = "https://github.com/tisonkun/mea"
license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/tisonkun/mea"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dev-dependencies]
pollster = { version = "0.3.0" }
tokio = { version = "1.41.0", features = ["full"] }
rust-version = "1.75.0"
64 changes: 62 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,63 @@
# Mea
# Make Easy Async (Mea)

Mea (Made easy async) provides async utilities that are runtime agnostic.
[![Crates.io][crates-badge]][crates-url]
[![Documentation][docs-badge]][docs-url]
[![MSRV 1.75][msrv-badge]](https://www.whatrustisit.com)
[![Apache 2.0 licensed][license-badge]][license-url]
[![Build Status][actions-badge]][actions-url]

[crates-badge]: https://img.shields.io/crates/v/mea.svg
[crates-url]: https://crates.io/crates/mea
[docs-badge]: https://docs.rs/mea/badge.svg
[msrv-badge]: https://img.shields.io/badge/MSRV-1.75-green?logo=rust
[docs-url]: https://docs.rs/mea
[license-badge]: https://img.shields.io/crates/l/mea
[license-url]: LICENSE
[actions-badge]: https://github.com/tisonkun/mea/actions/workflows/ci.yml/badge.svg
[actions-url]: https://github.com/tisonkun/mea/actions/workflows/ci.yml

## Overview

Mea provides async utilities that are runtime agnostic.

* WaitGroup

## Usage

### Default Features

Add the dependency to your `Cargo.toml` via:

```toml
mea = { version = "<version>" }
```

### `no_std`

By default, Mea enables the `std` feature and thus depends on the Rust standard library. To use Mea in a
`no_std` Rust environment, disable the default features and choose one of the following runtime features.

To use it on Linux, FreeBSD, Windows, and macOS, add the `sync-mutex` feature:

```toml
mea = { version = "<version>", default-features = false, features = ["sync-mutex"] }
```

To use it in an embedded environment, use [
`critical-section`](https://github.com/rust-embedded/critical-section/) and add one of its implementation libraries by yourself:

```toml
mea = { version = "<version>", default-features = false, features = ["sync-critical-section"] }
# choose a critical-section impl; for example, for single-core Cortex-M targets:
# cortex-m = { version = "0.7.6", features = ["critical-section-single-core"]}
```

## Minimum Rust version policy

This crate is built against the latest stable release, and its minimum supported rustc version is 1.75.0.

The policy is that the minimum Rust version required to use this crate can be increased in minor version updates. For example, if cronexpr 1.0 requires Rust 1.20.0, then cronexpr 1.0.z for all values of z will also require Rust 1.20.0 or newer. However, cronexpr 1.y for y > 0 may require a newer minimum version of Rust.

## License

This project is licensed under [Apache License, Version 2.0](LICENSE).
50 changes: 50 additions & 0 deletions mea/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2024 tison <wander4096@gmail.com>
#
# 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.

[package]
name = "mea"
version = "0.0.2"

categories = ["asynchronous", "concurrency", "no-std"]
description = "Async Rust utilities that are runtime agnostic."
documentation = "https://docs.rs/mea"
keywords = ["waitgroup", "asynchronous", "concurrency", "synchronization"]

edition.workspace = true
homepage.workspace = true
license.workspace = true
readme.workspace = true
repository.workspace = true
rust-version.workspace = true

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["std"]
std = []

# no_std impls for the internal sync primitives
sync-critical-section = ["dep:critical-section"]
sync-futex = ["dep:atomic-wait"]

[dependencies]
atomic-wait = { version = "1.1.0", optional = true }
critical-section = { version = "1.2.0", optional = true }

[dev-dependencies]
critical-section = { version = "1.2.0", features = ["std"] }
pollster = { version = "0.3.0" }
tokio = { version = "1.41.0", features = ["full"] }
39 changes: 39 additions & 0 deletions mea/src/internal/lock/impl_critical_section.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 tison <wander4096@gmail.com>
//
// 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.

use core::cell::RefCell;

pub(crate) struct Mutex<T>(critical_section::Mutex<RefCell<T>>);

impl<T> Mutex<T> {
#[inline]
pub(crate) const fn new(t: T) -> Self {
Self(critical_section::Mutex::new(RefCell::new(t)))
}

pub(crate) fn with<F, R>(&self, f: F) -> R
where
F: FnOnce(&mut T) -> R,
{
critical_section::with(|cs| {
let mut this = self
.0
.borrow(cs)
.try_borrow_mut()
// SAFETY: The outer with closure guarantees to borrow with token 'cs' exactly once.
.expect("[BUG] borrow the mutex multiple times");
f(&mut this)
})
}
}
Loading

0 comments on commit 2676138

Please sign in to comment.