Procedural macro to generate projection types
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Taiki Endo 2855546b11
Some checks failed
ci.yml / Release 1.1.12 (push) Failing after 0s
Release 1.1.12
2026-05-05 08:27:16 +00:00
.github ci,tools: Update config and script 2026-05-05 17:16:54 +09:00
examples Fix doc/comments 2026-04-15 23:59:55 +09:00
pin-project-internal Release 1.1.12 2026-05-05 08:27:16 +00:00
src Fix doc/comments 2026-04-15 23:59:55 +09:00
tests tests: Update lint test 2026-05-04 16:57:23 +09:00
tools ci,tools: Update config and script 2026-05-05 17:16:54 +09:00
.clippy.toml Apply deprecated_safe lint 2024-08-25 21:59:03 +09:00
.cspell.json tools: Update tidy.sh and related configs/markers 2025-02-03 06:20:55 +09:00
.deny.toml Update .deny.toml 2026-01-11 19:18:02 +09:00
.editorconfig tools: Update tidy.sh and related configs/markers 2025-02-03 06:20:55 +09:00
.git-blame-ignore-revs Add .git-blame-ignore-revs 2025-02-05 15:33:18 +09:00
.gitattributes gitattributes: Disable linguist detection for tools/tidy.sh 2025-02-05 15:56:24 +09:00
.gitignore Update script and CI config 2026-03-13 00:43:01 +09:00
.markdownlint-cli2.yaml Update comments 2025-02-08 15:22:31 +09:00
.rustfmt.toml rustfmt: Set hex_literal_case = "Upper" 2026-01-06 23:27:30 +09:00
.shellcheckrc Apply zizmor and update scripts 2025-12-05 01:53:11 +09:00
.taplo.toml tidy: Check TOML format 2024-02-23 03:09:53 +09:00
Cargo.toml Release 1.1.12 2026-05-05 08:27:16 +00:00
CHANGELOG.md Release 1.1.12 2026-05-05 08:27:16 +00:00
DEVELOPMENT.md mv tests/README.md DEVELOPMENT.md 2022-12-24 15:43:20 +09:00
LICENSE-APACHE Remove appendix from LICENCE-APACHE 2020-09-10 20:11:34 +09:00
LICENSE-MIT Initial commit 2019-01-08 23:39:18 +09:00
README.md Bump MSRV to 1.71 2026-02-09 23:50:01 +09:00

pin-project

crates.io docs.rs license msrv github actions

A crate for safe and ergonomic pin-projection.

Usage

Add this to your Cargo.toml:

[dependencies]
pin-project = "1"

Examples

#[pin_project] attribute creates projection types covering all the fields of struct or enum.

use std::pin::Pin;

use pin_project::pin_project;

#[pin_project]
struct Struct<T, U> {
    #[pin]
    pinned: T,
    unpinned: U,
}

impl<T, U> Struct<T, U> {
    fn method(self: Pin<&mut Self>) {
        let this = self.project();
        let _: Pin<&mut T> = this.pinned; // Pinned reference to the field
        let _: &mut U = this.unpinned; // Normal reference to the field
    }
}

code like this will be generated

To use #[pin_project] on enums, you need to name the projection type returned from the method.

use std::pin::Pin;

use pin_project::pin_project;

#[pin_project(project = EnumProj)]
enum Enum<T, U> {
    Pinned(#[pin] T),
    Unpinned(U),
}

impl<T, U> Enum<T, U> {
    fn method(self: Pin<&mut Self>) {
        match self.project() {
            EnumProj::Pinned(x) => {
                let _: Pin<&mut T> = x;
            }
            EnumProj::Unpinned(y) => {
                let _: &mut U = y;
            }
        }
    }
}

code like this will be generated

See #[pin_project] attribute for more details, and see examples directory for more examples and generated code.

  • pin-project-lite: A lightweight version of pin-project written with declarative macros.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

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.