Skip to content

Commit ff3825f

Browse files
committed
use safe_cell
1 parent 63b105d commit ff3825f

File tree

4 files changed

+64
-95
lines changed

4 files changed

+64
-95
lines changed

Cargo.lock

Lines changed: 9 additions & 67 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cynic-codegen/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ counter = "0.5"
2323
darling.workspace = true
2424
graphql-parser = "0.4.0"
2525
once_cell = "1.9.0"
26-
ouroboros = "0.18"
2726
proc-macro2 = "1.0"
2827
quote = "1.0"
28+
self_cell = { version = "1" }
2929
strsim = "0.10.0"
3030
syn = { workspace = true , features = ["visit-mut"] }
3131
thiserror = "1"
3232

33+
# Optional deps
3334
rkyv = { version = "0.7.41", features = ["validation"], optional = true }
3435

3536
[dev-dependencies]

cynic-codegen/src/schema/type_index/optimised.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use std::collections::HashMap;
33

44
use rkyv::Deserialize;
5+
use self_cell::self_cell;
56

67
use crate::schema::{
78
self,
@@ -31,19 +32,35 @@ impl Schema<'_, schema::Validated> {
3132
}
3233
}
3334

34-
#[ouroboros::self_referencing]
35+
self_cell! {
36+
struct AstCell {
37+
owner: Vec<u8>,
38+
39+
#[covariant]
40+
dependent: ArchiveRef,
41+
}
42+
}
43+
44+
type ArchiveRef<'a> = &'a ArchivedOptimisedTypes<'static>;
45+
3546
pub struct ArchiveBacked {
36-
data: Vec<u8>,
37-
#[borrows(data)]
38-
archived: &'this ArchivedOptimisedTypes<'static>,
47+
data: AstCell,
3948
}
4049

4150
impl ArchiveBacked {
4251
pub fn from_checked_data(data: Vec<u8>) -> Self {
43-
ArchiveBacked::new(data, |data| unsafe {
44-
// This is safe so long as we've already verified data
45-
rkyv::archived_root::<OptimisedTypes<'_>>(&data[..])
46-
})
52+
ArchiveBacked {
53+
data: AstCell::new(data, |data| {
54+
unsafe {
55+
// This is safe so long as we've already verified data
56+
rkyv::archived_root::<OptimisedTypes<'_>>(&data[..])
57+
}
58+
}),
59+
}
60+
}
61+
62+
fn archive(&self) -> ArchiveRef<'_> {
63+
self.data.borrow_dependent()
4764
}
4865
}
4966

@@ -55,7 +72,7 @@ impl super::TypeIndex for ArchiveBacked {
5572

5673
fn lookup_valid_type<'a>(&'a self, name: &str) -> Result<Type<'a>, SchemaError> {
5774
Ok(self
58-
.borrow_archived()
75+
.archive()
5976
.types
6077
.get(name)
6178
.ok_or_else(|| SchemaError::CouldNotFindType {
@@ -67,15 +84,15 @@ impl super::TypeIndex for ArchiveBacked {
6784

6885
fn root_types(&self) -> Result<schema::types::SchemaRoots<'_>, SchemaError> {
6986
Ok(self
70-
.borrow_archived()
87+
.archive()
7188
.schema_roots
7289
.deserialize(&mut rkyv::Infallible)
7390
.expect("infallible"))
7491
}
7592

7693
fn unsafe_lookup<'a>(&'a self, name: &str) -> Option<Type<'a>> {
7794
Some(
78-
self.borrow_archived()
95+
self.archive()
7996
.types
8097
.get(name)
8198
.unwrap()
@@ -85,7 +102,7 @@ impl super::TypeIndex for ArchiveBacked {
85102
}
86103

87104
fn unsafe_iter<'a>(&'a self) -> Box<dyn Iterator<Item = Type<'a>> + 'a> {
88-
Box::new(self.borrow_archived().types.values().map(|archived_type| {
105+
Box::new(self.archive().types.values().map(|archived_type| {
89106
archived_type
90107
.deserialize(&mut rkyv::Infallible)
91108
.expect("infallible")

cynic-codegen/src/schema/type_index/schema_backed.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
};
66

77
use once_cell::sync::Lazy;
8+
use self_cell::self_cell;
89

910
use crate::schema::{
1011
names::FieldName,
@@ -13,15 +14,22 @@ use crate::schema::{
1314
SchemaError,
1415
};
1516

16-
#[ouroboros::self_referencing]
17+
type TypeMap<'a> = HashMap<&'a str, &'a TypeDefinition>;
18+
19+
self_cell! {
20+
struct DocumentCell {
21+
owner: Document,
22+
23+
#[covariant]
24+
dependent: TypeMap,
25+
}
26+
}
27+
1728
pub struct SchemaBackedTypeIndex {
18-
document: Document,
1929
query_root: String,
2030
mutation_root: Option<String>,
2131
subscription_root: Option<String>,
22-
#[borrows(document)]
23-
#[covariant]
24-
types: HashMap<&'this str, &'this TypeDefinition>,
32+
data: DocumentCell,
2533
}
2634

2735
impl SchemaBackedTypeIndex {
@@ -41,12 +49,11 @@ impl SchemaBackedTypeIndex {
4149
}
4250
}
4351

44-
SchemaBackedTypeIndex::new(
45-
document,
52+
SchemaBackedTypeIndex {
4653
query_root,
4754
mutation_root,
4855
subscription_root,
49-
|document| {
56+
data: DocumentCell::new(document, |document| {
5057
let mut types = HashMap::new();
5158
for definition in &document.definitions {
5259
if let Definition::TypeDefinition(type_def) = definition {
@@ -57,8 +64,12 @@ impl SchemaBackedTypeIndex {
5764
types.insert(name_for_type(def), def);
5865
}
5966
types
60-
},
61-
)
67+
}),
68+
}
69+
}
70+
71+
fn borrow_types(&self) -> &HashMap<&str, &TypeDefinition> {
72+
self.data.borrow_dependent()
6273
}
6374
}
6475

@@ -82,17 +93,15 @@ impl super::TypeIndex for SchemaBackedTypeIndex {
8293

8394
fn root_types(&self) -> Result<SchemaRoots<'_>, SchemaError> {
8495
Ok(SchemaRoots {
85-
query: self
86-
.lookup_valid_type(self.borrow_query_root())?
87-
.try_into()?,
96+
query: self.lookup_valid_type(&self.query_root)?.try_into()?,
8897
mutation: self
89-
.borrow_mutation_root()
98+
.mutation_root
9099
.as_ref()
91100
.map(|name| ObjectType::try_from(self.lookup_valid_type(name)?))
92101
.transpose()?
93102
.or_else(|| ObjectType::try_from(self.lookup_valid_type("Mutation").ok()?).ok()),
94103
subscription: self
95-
.borrow_subscription_root()
104+
.subscription_root
96105
.as_ref()
97106
.map(|name| ObjectType::try_from(self.lookup_valid_type(name)?))
98107
.transpose()?

0 commit comments

Comments
 (0)