use borrowing_field::BorrowingFieldVisitor; use borrowing_param::BorrowingParamVisitor;
pubmod borrowing_field; pubmod borrowing_param;
/// A method exposed to Diplomat. /// Used for representing both free functions ([`crate::ast::Function`]) and struct methods ([`crate::ast::Method`]). /// The only difference between a free function and a struct method in this struct is that [`Self::param_self`] will always be `None` for a free function. #[derive(Debug)] #[non_exhaustive] pubstruct Method { /// Documentation specified on the method pub docs: Docs, /// The name of the method as initially declared. pub name: IdentBuf, /// The name of the generated `extern "C"` function pub abi_name: IdentBuf, /// The lifetimes introduced in this method and surrounding impl block. pub lifetime_env: LifetimeEnv,
/// An &self, &mut self, or Self parameter pub param_self: Option<ParamSelf>, /// The parameters of the method pub params: Vec<Param>, /// The output type, including whether it returns a Result/Option/Writeable/etc pub output: ReturnType, /// Resolved (and inherited) diplomat::attr attributes on this method pub attrs: Attrs,
}
pubtrait CallbackInstantiationFunctionality: Sealed { #[allow(clippy::result_unit_err)] fn get_inputs(&self) -> Result<&[CallbackParam], ()>; // the types of the parameters #[allow(clippy::result_unit_err)] fn get_output_type(&self) -> Result<&ReturnType<InputOnly>, ()>;
}
#[derive(Debug, Clone)] #[non_exhaustive] // Note: we do not support borrowing across callbacks pubstruct Callback { pub param_self: Option<TraitParamSelf>, // this is None for callbacks as method arguments pub params: Vec<CallbackParam>, pub output: Box<ReturnType<InputOnly>>, // this will be used in Rust (note: can technically be a callback, or void) pub name: Option<IdentBuf>, pub attrs: Option<Attrs>, pub docs: Option<Docs>,
}
/// Type that the method returns. #[derive(Debug, Clone)] #[non_exhaustive] pubenum SuccessType<P: super::TyPosition = OutputOnly> { /// Conceptually returns a string, which gets written to the `write: DiplomatWrite` argument
Write, /// A Diplomat type. Some types can be outputs, but not inputs, which is expressed by the `OutType` parameter.
OutType(Type<P>), /// A `()` type in Rust.
Unit,
}
/// Whether or not the method returns a value or a result. #[derive(Debug, Clone)] #[allow(clippy::exhaustive_enums)] // this only exists for fallible/infallible, breaking changes for more complex returns are ok pubenum ReturnType<P: super::TyPosition = OutputOnly> {
Infallible(SuccessType<P>),
Fallible(SuccessType<P>, Option<Type<P>>),
Nullable(SuccessType<P>),
}
/// The `self` parameter of a method. #[derive(Debug)] #[non_exhaustive] pubstruct ParamSelf { pub ty: SelfType, pub attrs: Attrs,
}
/// A parameter in a method. #[derive(Debug)] #[non_exhaustive] pubstruct Param { pub name: IdentBuf, pub ty: Type<InputOnly>, pub attrs: Attrs,
}
/// A parameter in a callback /// No name, since all we get is the callback type signature #[derive(Debug, Clone)] #[non_exhaustive] pubstruct CallbackParam { pub ty: Type<OutputOnly>, pub name: Option<IdentBuf>,
}
impl SuccessType { /// Returns whether the variant is `Write`. pubfn is_write(&self) -> bool {
matches!(self, SuccessType::Write)
}
/// Returns whether the variant is `Unit`. pubfn is_unit(&self) -> bool {
matches!(self, SuccessType::Unit)
}
impl ReturnType { /// Returns `true` if the FFI function returns `void`. Not that this is different from `is_unit`, /// which will be true for `DiplomatResult<(), E>` and false for infallible write. pubfn is_ffi_unit(&self) -> bool {
matches!( self,
ReturnType::Infallible(SuccessType::Unit | SuccessType::Write)
)
}
/// The "main" return type of this function: the Ok, Some, or regular type pubfn success_type(&self) -> &SuccessType { match &self { Self::Infallible(s) => s, Self::Fallible(s, _) => s, Self::Nullable(s) => s,
}
}
/// Get the list of method lifetimes actually used by the method return type /// /// Most input lifetimes aren't actually used. An input lifetime is generated /// for each borrowing parameter but is only important if we use it in the return. pubfn used_method_lifetimes(&self) -> BTreeSet<Lifetime> { letmut set = BTreeSet::new();
letmut add_to_set = |ty: &OutType| { for lt in ty.lifetimes() { iflet MaybeStatic::NonStatic(lt) = lt {
set.insert(lt);
}
}
};
/// Return the number of fields and leaves that will show up in the [`BorrowingFieldVisitor`]. /// /// This method is used to calculate how much space to allocate upfront. fn field_leaf_lifetime_counts(&self, tcx: &TypeContext) -> (usize, usize) { matchself.ty {
SelfType::Opaque(_) => (1, 1),
SelfType::Struct(ref ty) => ty.resolve(tcx).fields.iter().fold((1, 0), |acc, field| { let inner = field.ty.field_leaf_lifetime_counts(tcx);
(acc.0 + inner.0, acc.1 + inner.1)
}),
SelfType::Enum(_) => (0, 0),
}
}
}
impl Method { /// Returns a fresh [`Lifetimes`] corresponding to `self`. pubfn method_lifetimes(&self) -> Lifetimes { self.lifetime_env.lifetimes()
}
/// Returns a new [`BorrowingParamVisitor`], which can *shallowly* link output lifetimes /// to the parameters they borrow from. /// /// This is useful for backends which wish to have lifetime codegen for methods only handle the local /// method lifetime, and delegate to generated code on structs for handling the internals of struct lifetimes. /// /// `force_include_slices` is right now *just* for the JS backend. /// Because the JS backend requires us to know information about the allocation of each slice, /// then we need to grab that information in the [`BorrowingParamVisitor`]. /// See [`BorrowingParamVisitor::new`] for more. pubfn borrowing_param_visitor<'tcx>(
&'tcx self,
tcx: &'tcx TypeContext,
force_include_slices: bool,
) -> BorrowingParamVisitor<'tcx> {
BorrowingParamVisitor::new(self, tcx, force_include_slices)
}
/// Returns a new [`BorrowingFieldVisitor`], which allocates memory to /// efficiently represent all fields (and their paths!) of the inputs that /// have a lifetime. /// /// This is useful for backends which wish to "splat out" lifetime edge codegen for methods, /// linking each borrowed input param/field (however deep it may be in a struct) to a borrowed output param/field. /// /// ```ignore /// # use std::collections::BTreeMap; /// let visitor = method.borrowing_field_visitor(&tcx, "this".ck().unwrap()); /// let mut map = BTreeMap::new(); /// visitor.visit_borrowing_fields(|lifetime, field| { /// map.entry(lifetime).or_default().push(field); /// }) /// ``` pubfn borrowing_field_visitor<'m>(
&'m self,
tcx: &'m TypeContext,
self_name: &'m Ident,
) -> BorrowingFieldVisitor<'m> {
BorrowingFieldVisitor::new(self, tcx, self_name)
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.