1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
//! Generates C# bindings for [Interoptopus](https://github.com/ralfbiedert/interoptopus). //! //! //! ## Usage //! //! In your library or a support project add this: //! //! ``` //! # mod my_crate { use interoptopus::{Library}; pub fn ffi_inventory() -> Library { todo!() } } //! use my_crate::ffi_inventory; //! //! #[test] //! fn generate_csharp_bindings() { //! use interoptopus::Interop; //! use interoptopus_backend_csharp::{Generator, CSharpWriter, Config}; //! //! // Converts an `ffi_inventory()` into C# interop definitions. //! Generator::new(Config::default(), ffi_inventory()).write_to("Interop.cs") //! } //! ``` //! //! And we might produce something like this: //! //! ```cs //! using System; //! using System.Runtime.InteropServices; //! //! namespace My.Company //! { //! public static class InteropClass //! { //! public const string NativeLib = "hello_world"; //! //! /// A function which does something with the vector. //! [DllImport(NativeLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "my_game_function")] //! public static extern Vec3f32 my_game_function(ref Vec3f32 input); //! } //! //! [Serializable] //! [StructLayout(LayoutKind.Sequential)] //! public partial struct Vec3f32 //! { //! public float x; //! public float y; //! public float z; //! } //! } //! ``` use interoptopus::writer::IndentWriter; use interoptopus::Interop; use interoptopus::{Error, Library}; mod config; mod converter; mod writer; pub use crate::config::Config; pub use crate::converter::{CSharpTypeConverter, Converter}; pub use crate::writer::CSharpWriter; /// **Start here**, main converter implementing [`Interop`]. pub struct Generator { config: Config, library: Library, converter: Converter, } impl Generator { pub fn new(config: Config, library: Library) -> Self { Self { config, library, converter: Converter {}, } } } impl Interop for Generator { fn write_to(&self, w: &mut IndentWriter) -> Result<(), Error> { self.write_all(w) } } impl CSharpWriter for Generator { fn config(&self) -> &Config { &self.config } fn library(&self) -> &Library { &self.library } fn converter(&self) -> &Converter { &self.converter } }