Skip to main content

wiggle_generate/
lifetimes.rs

1use proc_macro2::TokenStream;
2use quote::quote;
3
4pub trait LifetimeExt {
5    fn needs_lifetime(&self) -> bool;
6}
7
8impl LifetimeExt for witx::TypeRef {
9    fn needs_lifetime(&self) -> bool {
10        self.type_().needs_lifetime()
11    }
12}
13
14impl LifetimeExt for witx::Type {
15    fn needs_lifetime(&self) -> bool {
16        match self {
17            witx::Type::Builtin(b) => b.needs_lifetime(),
18            witx::Type::Record(s) => s.needs_lifetime(),
19            witx::Type::Variant(u) => u.needs_lifetime(),
20            witx::Type::Handle { .. } => false,
21            witx::Type::Pointer { .. }
22            | witx::Type::ConstPointer { .. }
23            | witx::Type::List { .. } => true,
24        }
25    }
26}
27
28impl LifetimeExt for witx::BuiltinType {
29    fn needs_lifetime(&self) -> bool {
30        false
31    }
32}
33
34impl LifetimeExt for witx::RecordDatatype {
35    fn needs_lifetime(&self) -> bool {
36        self.members.iter().any(|m| m.tref.needs_lifetime())
37    }
38}
39
40impl LifetimeExt for witx::Variant {
41    fn needs_lifetime(&self) -> bool {
42        self.cases
43            .iter()
44            .any(|m| m.tref.as_ref().map(|t| t.needs_lifetime()).unwrap_or(false))
45    }
46}
47
48pub fn anon_lifetime() -> TokenStream {
49    quote!('_)
50}