csbindgen/dotnet-sandbox/Program.cs

209 lines
4.5 KiB
C#
Raw Normal View History

2023-03-04 05:39:45 +00:00

// See https://aka.ms/new-console-template for more information
2023-02-27 22:18:10 +00:00
//using Csbindgen;
2023-02-28 01:22:34 +00:00
using CsBindgen;
2023-03-04 05:39:45 +00:00
using System.Buffers.Text;
2023-02-28 09:39:52 +00:00
using System.Reflection;
2023-02-26 18:31:44 +00:00
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
2023-03-04 05:39:45 +00:00
using System.Text;
2023-02-26 18:31:44 +00:00
unsafe
{
2023-03-06 09:06:52 +00:00
2023-03-06 14:15:17 +00:00
2023-03-08 09:41:46 +00:00
//NativeMethods.call_bindgen_lz4();
var str = "foobarbaz:あいうえお"; // JPN(Unicode)
fixed (char* p = str)
{
NativeMethods.csharp_to_rust_string((ushort*)p, str.Length);
}
var str2 = Encoding.UTF8.GetBytes("あいうえお:foobarbaz");
fixed (byte* p = str2)
{
NativeMethods.csharp_to_rust_utf8(p, str2.Length);
}
var bytes = new byte[] { 1, 10, 100, 255 };
fixed (byte* p = bytes)
{
NativeMethods.csharp_to_rust_bytes(p, bytes.Length);
}
//NativeMethods.csharp_to_rust_utf8
//NativeMethods.alias_test1(null);
2023-03-06 09:06:52 +00:00
2023-03-06 20:00:14 +00:00
// C# -> Rust, pass static UnmanagedCallersOnly method with `&`
2023-03-05 06:30:41 +00:00
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
2023-03-06 20:00:14 +00:00
static int Sum(int x, int y) => x + y;
2023-03-04 16:03:33 +00:00
2023-03-08 09:41:46 +00:00
NativeMethods.csharp_to_rust(&Sum);
2023-03-04 16:03:33 +00:00
2023-03-06 20:00:14 +00:00
// Rust -> C#, get typed delegate*
2023-03-08 09:41:46 +00:00
var f = NativeMethods.rust_to_csharp();
2023-03-05 08:18:52 +00:00
2023-03-06 20:00:14 +00:00
var v = f(20, 30);
Console.WriteLine(v); // 50
2023-03-05 06:30:41 +00:00
2023-03-05 08:18:52 +00:00
2023-03-05 06:30:41 +00:00
2023-03-06 20:00:14 +00:00
// var tako = LibRust.callback_test(&Method);
//var tako = LibRust.callback_test(&Method);
//Console.WriteLine(tako);
//var cc = LibRust.enum_test(IntEnumTest.C);
//Console.WriteLine(cc);
2023-03-08 09:41:46 +00:00
// var context = LibRust.create_context();
2023-03-06 20:00:14 +00:00
2023-03-08 09:41:46 +00:00
// // do anything...
2023-03-06 20:00:14 +00:00
2023-03-08 09:41:46 +00:00
// LibRust.delete_context(context);
2023-03-06 20:00:14 +00:00
2023-03-08 09:41:46 +00:00
//var ctx = LibRust.create_counter_context(); // ctx = void*
2023-03-06 20:00:14 +00:00
2023-03-08 09:41:46 +00:00
//LibRust.insert_counter_context(ctx, 10);
//LibRust.insert_counter_context(ctx, 20);
//LibRust.delete_counter_context(ctx);
2023-03-06 20:00:14 +00:00
2023-03-05 06:30:41 +00:00
//LibRust.insert_counter_context(ctx, 20);
//LibRust.insert_counter_context(ctx, 30);
//LibRust.insert_counter_context(ctx, 99);
//LibRust.delete_counter_context(ctx);
2023-03-04 16:03:33 +00:00
2023-03-06 20:00:14 +00:00
// var cString = LibRust.alloc_c_string();
2023-03-04 16:03:33 +00:00
//var u8String = LibRust.alloc_u8_string();
//var u8Buffer = LibRust.alloc_u8_buffer();
//var i32Buffer = LibRust.alloc_i32_buffer();
//try
//{
// var str = new String((sbyte*)cString);
// Console.WriteLine(str);
// Console.WriteLine("----");
// var str2 = Encoding.UTF8.GetString(u8String->AsSpan());
// Console.WriteLine(str2);
// Console.WriteLine("----");
// var buffer3 = u8Buffer->AsSpan();
// foreach (var item in buffer3)
// {
// Console.WriteLine(item);
// }
// Console.WriteLine("----");
// var i32Span = i32Buffer->AsSpan<int>();
// foreach (var item in i32Span)
// {
// Console.WriteLine(item);
// }
//}
//finally
//{
// LibRust.free_c_string(cString);
// LibRust.free_u8_string(u8String);
// LibRust.free_u8_buffer(u8Buffer);
// LibRust.free_i32_buffer(i32Buffer);
//}
//var buf = LibRust.return_raw_buffer();
//try
//{
// var span = buf->AsSpan();
2023-03-06 20:00:14 +00:00
// var str = Encoding.UTF8.GetString(span);
// Console.WriteLine(str);
// //foreach (var item in span)
// //{
// // Console.WriteLine(item);
// //}
//}
//finally
//{
// LibRust.delete_raw_buffer(buf);
//}
2023-02-26 18:31:44 +00:00
}
2023-02-28 09:39:52 +00:00
public static unsafe partial class LibraryImportNativeMethods
{
const string __DllName = "csbindgen_tests";
2023-02-28 09:39:52 +00:00
[LibraryImport(__DllName, EntryPoint = "my_bool")]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool my_bool([MarshalAs(UnmanagedType.U1)] bool x, [MarshalAs(UnmanagedType.U1)] bool y, [MarshalAs(UnmanagedType.U1)] bool z, bool* xr, bool* yr, bool* zr);
2023-02-28 09:39:52 +00:00
2023-03-04 05:39:45 +00:00
2023-03-06 20:00:14 +00:00
//[LibraryImport(__DllName)]
//public static partial void foo(Foo f);
2023-03-05 08:18:52 +00:00
2023-03-06 20:00:14 +00:00
[LibraryImport(__DllName, EntryPoint = "nullable_callback_test")]
public static partial int nullable_callback_test([MarshalAs(UnmanagedType.FunctionPtr)] Func<int, int> cb);
2023-03-05 08:18:52 +00:00
2023-03-06 20:00:14 +00:00
[LibraryImport(__DllName, EntryPoint = "nullable_callback_test")]
public static partial int nullable_callback_test2(delegate* unmanaged[Cdecl]<int, int> cb);
2023-03-05 08:18:52 +00:00
2023-03-04 05:39:45 +00:00
}
public struct Foo
{
[MarshalAs(UnmanagedType.U1)] public bool A;
}
namespace CsBindgen
{
partial struct ByteBuffer
{
public unsafe Span<byte> AsSpan()
{
return new Span<byte>(ptr, length);
}
public unsafe Span<T> AsSpan<T>()
{
return MemoryMarshal.CreateSpan(ref Unsafe.AsRef<T>(ptr), length / Unsafe.SizeOf<T>());
}
}
2023-03-05 08:18:52 +00:00
}