Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite macros using syn #20

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ jobs:
toolchain: ${{ matrix.toolchain }}
components: clippy
- name: cargo clippy
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
run: cargo clippy -- -D warnings
doc:
runs-on: ubuntu-latest
name: nightly / doc
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/nostd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ jobs:
run: rustup target add ${{ matrix.target }}
- name: cargo check
run: cargo check --target ${{ matrix.target }} --no-default-features
env:
RUSTFLAGS: "-Dwarnings"
18 changes: 14 additions & 4 deletions .github/workflows/safety.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,24 @@ jobs:
run: cargo test --lib --tests --all-features --target x86_64-unknown-linux-gnu
env:
ASAN_OPTIONS: "detect_odr_violation=0:detect_leaks=0"
RUSTFLAGS: "--cfg NO_UI_TESTS --cfg NO_ALLOC_FAIL_TESTS -Z sanitizer=address"
RUSTFLAGS: "--cfg NO_UI_TESTS --cfg NO_ALLOC_FAIL_TESTS -Z sanitizer=address -Dwarnings"
- name: cargo test -Zsanitizer=leak
if: always()
run: cargo test --all-features --target x86_64-unknown-linux-gnu
env:
LSAN_OPTIONS: "suppressions=lsan-suppressions.txt"
RUSTFLAGS: "--cfg NO_UI_TESTS --cfg NO_ALLOC_FAIL_TESTS -Z sanitizer=leak"
RUSTFLAGS: "--cfg NO_UI_TESTS --cfg NO_ALLOC_FAIL_TESTS -Z sanitizer=leak -Dwarnings"
miri:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
flags: [
"",
"-Zmiri-tree-borrows",
"-Zmiri-strict-provenance",
"-Zmiri-tree-borrows -Zmiri-strict-provenance"
]
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -48,10 +57,11 @@ jobs:
with:
toolchain: ${{ env.NIGHTLY }}
components: miri
- name: cargo miri test
- name: ${{ matrix.flags }} cargo miri test
run: cargo miri test
env:
MIRIFLAGS: ""
RUSTFLAGS: "-Dwarnings"
MIRIFLAGS: ${{ matrix.flags }}
# loom:
# runs-on: ubuntu-latest
# steps:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/scheduled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
run: cargo generate-lockfile
- name: cargo test --locked
run: cargo test --locked --all-features --all-targets
env:
RUSTFLAGS: "-Dwarnings"
# https://twitter.com/alcuadrado/status/1571291687837732873
update:
runs-on: ubuntu-latest
Expand All @@ -46,4 +48,4 @@ jobs:
- name: cargo test
run: cargo test --locked --all-features --all-targets
env:
RUSTFLAGS: -D deprecated
RUSTFLAGS: -Ddeprecated -Dwarnings
8 changes: 7 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ jobs:
# https://twitter.com/jonhoo/status/1571290371124260865
- name: cargo test --locked
run: cargo test --locked --all-features --all-targets
env:
RUSTFLAGS: "-Dwarnings"
# https://github.com/rust-lang/cargo/issues/6669
- name: cargo test --doc
run: cargo test --locked --all-features --doc
env:
RUSTFLAGS: "-Dwarnings"
os-check:
runs-on: ${{ matrix.os }}
name: ${{ matrix.os }} / nightly
Expand All @@ -53,4 +57,6 @@ jobs:
if: hashFiles('Cargo.lock') == ''
run: cargo generate-lockfile
- name: cargo test
run: cargo test --locked --all-features --all-targets
run: cargo test --locked --all-features --all-targets
env:
RUSTFLAGS: "-Dwarnings"
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ and thus is a prime candidate for using this library.
### Using the [`pin_init!`] macro

If you want to use [`PinInit`], then you will have to annotate your `struct` with
`#[`[`pin_data`]`]`. It is a macro that uses `#[pin]` as a marker for
[`#[pin_data]`](pin_data). It is a macro that uses `#[pin]` as a marker for
[structurally pinned fields]. After doing this, you can then create an in-place constructor via
[`pin_init!`]. The syntax is almost the same as normal `struct` initializers. The difference is
that you need to write `<-` instead of `:` for fields that you want to initialize in-place.

```rust
use pinned_init::*;
use pinned_init::{pin_data, pin_init};
#[pin_data]
struct Foo {
#[pin]
Expand All @@ -87,6 +87,8 @@ let foo = pin_init!(Foo {
(or just the stack) to actually initialize a `Foo`:

```rust
use pinned_init::InPlaceInit;

let foo: Result<Pin<Box<Foo>>, _> = Box::pin_init(foo);
```

Expand All @@ -98,12 +100,16 @@ Many types that use this library supply a function/macro that returns an initial
the above method only works for types where you can access the fields.

```rust
use pinned_init::InPlaceInit;

let mtx: Result<Pin<Arc<CMutex<usize>>>, _> = Arc::pin_init(CMutex::new(42));
```

To declare an init macro/function you just return an [`impl PinInit<T, E>`]:

```rust
use pinned_init::{pin_data, try_pin_init, PinInit, InPlaceInit};

#[pin_data]
struct DriverData {
#[pin]
Expand Down Expand Up @@ -136,7 +142,7 @@ actually does the initialization in the correct way. Here are the things to look
`slot` gets called.

```rust
use pinned_init::*;
use pinned_init::{pin_data, pinned_drop, pin_init_from_closure, PinInit};
use core::{ptr::addr_of_mut, marker::PhantomPinned, cell::UnsafeCell, pin::Pin};
mod bindings {
extern "C" {
Expand Down
2 changes: 0 additions & 2 deletions examples/big_struct_in_place.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![feature(allocator_api)]

use std::convert::Infallible;

use pinned_init::*;

// Struct with size over 1GiB
Expand Down
2 changes: 2 additions & 0 deletions examples/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unused_attributes)]
#![feature(allocator_api)]

use core::convert::Infallible;
Expand All @@ -18,4 +19,5 @@ impl From<AllocError> for Error {
}
}

#[allow(dead_code)]
fn main() {}
4 changes: 4 additions & 0 deletions examples/mutex.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![allow(unused_attributes)]
#![feature(allocator_api)]

use core::{
cell::{Cell, UnsafeCell},
marker::PhantomPinned,
Expand All @@ -16,6 +18,7 @@ use pinned_init::*;
#[allow(unused_attributes)]
#[path = "./linked_list.rs"]
pub mod linked_list;
#[allow(unused_imports)]
pub use linked_list::Error;
use linked_list::*;

Expand Down Expand Up @@ -100,6 +103,7 @@ impl<T> CMutex<T> {
}
}

#[allow(dead_code)]
pub fn get_data_mut(self: Pin<&mut Self>) -> &mut T {
// SAFETY: we have an exclusive reference and thus nobody has access to data.
unsafe { &mut *self.data.get() }
Expand Down
2 changes: 2 additions & 0 deletions examples/pthread_mutex.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// inspired by https://github.com/nbdd0121/pin-init/blob/trunk/examples/pthread_mutex.rs
#![allow(dead_code)]
#![feature(allocator_api)]

#[cfg(not(windows))]
mod pthread_mtx {
use core::{
Expand Down
20 changes: 16 additions & 4 deletions pinned-init-macro/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pinned-init-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ proc-macro = true
[dependencies]
quote = "1.0"
proc-macro2 = "1.0"
syn = { version = "2.0", features = ["full", "visit-mut", "printing"] }
Loading