+/// Writes out a C-callable concrete Option<A> struct and utility methods
+pub fn write_option_block<W: std::io::Write>(w: &mut W, mangled_container: &str, inner_type: &str, clonable: bool) {
+ writeln!(w, "#[repr(C)]").unwrap();
+ if clonable {
+ writeln!(w, "#[derive(Clone)]").unwrap();
+ }
+ writeln!(w, "/// An enum which can either contain a {} or not", inner_type).unwrap();
+ writeln!(w, "pub enum {} {{", mangled_container).unwrap();
+ writeln!(w, "\t/// When we're in this state, this {} contains a {}", mangled_container, inner_type).unwrap();
+ if inner_type != "" {
+ writeln!(w, "\tSome({}),", inner_type).unwrap();
+ } else {
+ writeln!(w, "\tSome,").unwrap();
+ }
+ writeln!(w, "\t/// When we're in this state, this {} contains nothing", mangled_container).unwrap();
+ writeln!(w, "\tNone").unwrap();
+ writeln!(w, "}}").unwrap();
+
+ writeln!(w, "impl {} {{", mangled_container).unwrap();
+ writeln!(w, "\t#[allow(unused)] pub(crate) fn is_some(&self) -> bool {{").unwrap();
+ writeln!(w, "\t\tif let Self::None = self {{ false }} else {{ true }}").unwrap();
+ writeln!(w, "\t}}").unwrap();
+ writeln!(w, "\t#[allow(unused)] pub(crate) fn is_none(&self) -> bool {{").unwrap();
+ writeln!(w, "\t\t!self.is_some()").unwrap();
+ writeln!(w, "\t}}").unwrap();
+ if inner_type != "" {
+ writeln!(w, "\t#[allow(unused)] pub(crate) fn take(mut self) -> {} {{", inner_type).unwrap();
+ writeln!(w, "\t\tif let Self::Some(v) = self {{ v }} else {{ unreachable!() }}").unwrap();
+ writeln!(w, "\t}}").unwrap();
+ }
+ writeln!(w, "}}").unwrap();
+
+ writeln!(w, "#[no_mangle]").unwrap();
+ writeln!(w, "/// Constructs a new {} containing a {}", mangled_container, inner_type).unwrap();
+ if inner_type != "" {
+ writeln!(w, "pub extern \"C\" fn {}_some(o: {}) -> {} {{", mangled_container, inner_type, mangled_container).unwrap();
+ writeln!(w, "\t{}::Some(o)", mangled_container).unwrap();
+ } else {
+ writeln!(w, "pub extern \"C\" fn {}_some() -> {} {{", mangled_container, mangled_container).unwrap();
+ writeln!(w, "\t{}::Some", mangled_container).unwrap();
+ }
+ writeln!(w, "}}").unwrap();
+
+ writeln!(w, "#[no_mangle]").unwrap();
+ writeln!(w, "/// Constructs a new {} containing nothing", mangled_container).unwrap();
+ writeln!(w, "pub extern \"C\" fn {}_none() -> {} {{", mangled_container, mangled_container).unwrap();
+ writeln!(w, "\t{}::None", mangled_container).unwrap();
+ writeln!(w, "}}").unwrap();
+
+ writeln!(w, "#[no_mangle]").unwrap();
+ writeln!(w, "/// Frees any resources associated with the {}, if we are in the Some state", inner_type).unwrap();
+ writeln!(w, "pub extern \"C\" fn {}_free(_res: {}) {{ }}", mangled_container, mangled_container).unwrap();
+ if clonable {
+ writeln!(w, "#[no_mangle]").unwrap();
+ writeln!(w, "/// Creates a new {} which has the same data as `orig`", mangled_container).unwrap();
+ writeln!(w, "/// but with all dynamically-allocated buffers duplicated in new buffers.").unwrap();
+ writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{ Clone::clone(&orig) }}", mangled_container, mangled_container, mangled_container).unwrap();
+ }
+}
+
+/// Prints the docs from a given attribute list unless its tagged no export
+pub fn writeln_fn_docs<'a, W: std::io::Write, I>(w: &mut W, attrs: &[syn::Attribute], prefix: &str, types: &mut TypeResolver, generics: Option<&GenericTypes>, args: I, ret: &syn::ReturnType) where I: Iterator<Item = &'a syn::FnArg> {
+ writeln_docs_impl(w, attrs, prefix, Some((types, generics,
+ args.filter_map(|arg| if let syn::FnArg::Typed(ty) = arg {
+ if let syn::Pat::Ident(id) = &*ty.pat {
+ Some((id.ident.to_string(), &*ty.ty))
+ } else { unimplemented!() }
+ } else { None }),
+ if let syn::ReturnType::Type(_, ty) = ret { Some(&**ty) } else { None },
+ None
+ )));
+}
+