-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding OpaqueType instead of OpaqueStruct, and adding tests
- Loading branch information
Ellen Arteca
committed
Dec 10, 2024
1 parent
6434f0b
commit b6dee05
Showing
25 changed files
with
500 additions
and
86 deletions.
There are no files selected for viewing
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
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,64 @@ | ||
use serde::Serialize; | ||
|
||
use super::docs::Docs; | ||
use super::{Attrs, Ident, LifetimeEnv, Method, Mutability}; | ||
|
||
/// A type annotated with [`diplomat::opaque`] whose fields/variants are not visible. | ||
/// Opaque types cannot be passed by-value across the FFI boundary, so they | ||
/// must be boxed or passed as references. | ||
#[derive(Clone, Serialize, Debug, Hash, PartialEq, Eq)] | ||
#[non_exhaustive] | ||
pub struct OpaqueType { | ||
pub name: Ident, | ||
pub docs: Docs, | ||
pub lifetimes: LifetimeEnv, | ||
pub methods: Vec<Method>, | ||
pub mutability: Mutability, | ||
pub attrs: Attrs, | ||
/// The ABI name of the generated destructor | ||
pub dtor_abi_name: Ident, | ||
} | ||
|
||
impl OpaqueType { | ||
/// Extract a [`OpaqueType`] metadata value from an AST node representing a struct. | ||
pub fn new_struct( | ||
strct: &syn::ItemStruct, | ||
mutability: Mutability, | ||
parent_attrs: &Attrs, | ||
) -> Self { | ||
let mut attrs = parent_attrs.clone(); | ||
attrs.add_attrs(&strct.attrs); | ||
let name = Ident::from(&strct.ident); | ||
OpaqueType { | ||
dtor_abi_name: Self::dtor_abi_name(&name, &attrs), | ||
name, | ||
docs: Docs::from_attrs(&strct.attrs), | ||
lifetimes: LifetimeEnv::from_struct_item(strct, &[]), | ||
methods: vec![], | ||
mutability, | ||
attrs, | ||
} | ||
} | ||
|
||
/// Extract a [`OpaqueType`] metadata value from an AST node representing an enum. | ||
pub fn new_enum(enm: &syn::ItemEnum, mutability: Mutability, parent_attrs: &Attrs) -> Self { | ||
let mut attrs = parent_attrs.clone(); | ||
attrs.add_attrs(&enm.attrs); | ||
let name = Ident::from(&enm.ident); | ||
OpaqueType { | ||
dtor_abi_name: Self::dtor_abi_name(&name, &attrs), | ||
name, | ||
docs: Docs::from_attrs(&enm.attrs), | ||
lifetimes: LifetimeEnv::from_enum_item(enm, &[]), | ||
methods: vec![], | ||
mutability, | ||
attrs, | ||
} | ||
} | ||
|
||
fn dtor_abi_name(name: &Ident, attrs: &Attrs) -> Ident { | ||
let dtor_abi_name = format!("{}_destroy", name); | ||
let dtor_abi_name = String::from(attrs.abi_rename.apply(dtor_abi_name.into())); | ||
Ident::from(dtor_abi_name) | ||
} | ||
} |
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
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
4 changes: 3 additions & 1 deletion
4
core/src/hir/snapshots/diplomat_core__hir__type_context__tests__opaque_ffi.snap
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
--- | ||
source: core/src/hir/type_context.rs | ||
assertion_line: 691 | ||
expression: output | ||
--- | ||
Lowering error in MyOpaqueEnum::new_broken: Opaque passed by value in input: MyOpaqueEnum | ||
Lowering error in MyOpaqueEnum::do_thing_broken: Method `MyOpaqueEnum_do_thing_broken` takes an opaque by value as the self parameter, but opaques as inputs must be behind refs | ||
Lowering error in MyOpaqueStruct::new_broken: Opaque passed by value in input: MyOpaqueStruct | ||
Lowering error in MyOpaqueStruct::do_thing_broken: Method `MyOpaqueStruct_do_thing_broken` takes an opaque by value as the self parameter, but opaques as inputs must be behind refs | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.