-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a keepalive abstraction (#10764)
This effectively emulates what pyo3's old GIL pool was doing, but we'll use it in a far more targetted manner.
- Loading branch information
Showing
5 changed files
with
65 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "cryptography-keepalive" | ||
version = "0.1.0" | ||
authors = ["The cryptography developers <cryptography-dev@python.org>"] | ||
edition = "2021" | ||
publish = false | ||
# This specifies the MSRV | ||
rust-version = "1.65.0" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// This file is dual licensed under the terms of the Apache License, Version | ||
// 2.0, and the BSD License. See the LICENSE file in the root of this repository | ||
// for complete details. | ||
|
||
#![deny(rust_2018_idioms, clippy::undocumented_unsafe_blocks)] | ||
|
||
use std::cell::UnsafeCell; | ||
use std::ops::Deref; | ||
|
||
pub struct KeepAlive<T: StableDeref> { | ||
values: UnsafeCell<Vec<T>>, | ||
} | ||
|
||
/// # Safety | ||
/// Implementors of this trait must ensure that the value returned by | ||
/// `deref()` must remain valid, even if `self` is moved. | ||
pub unsafe trait StableDeref: Deref {} | ||
// SAFETY: `Vec`'s data is on the heap, so as long as it's not mutated, the | ||
// slice returned by `deref` remains valid. | ||
unsafe impl<T> StableDeref for Vec<T> {} | ||
|
||
#[allow(clippy::new_without_default)] | ||
impl<T: StableDeref> KeepAlive<T> { | ||
pub fn new() -> Self { | ||
KeepAlive { | ||
values: UnsafeCell::new(vec![]), | ||
} | ||
} | ||
|
||
pub fn add(&self, v: T) -> &T::Target { | ||
// SAFETY: We only ever append to `self.values`, which, when combined | ||
// with the invariants of `StableDeref`, means that the result of | ||
// `deref()` will always be valid for the lifetime of `&self`. | ||
unsafe { | ||
let values = &mut *self.values.get(); | ||
values.push(v); | ||
values.last().unwrap().deref() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters