Skip to content

Commit e9edf8c

Browse files
committed
Make Value type support in serializer more generic.
1 parent 5c6f6b6 commit e9edf8c

File tree

3 files changed

+287
-205
lines changed

3 files changed

+287
-205
lines changed

BTDB.SourceGenerator.Sample/Examples.cs

+53-23
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,67 @@
66
using BTDB.Serialization;
77
using Microsoft.AspNetCore.Http;
88

9-
var builder = new ContainerBuilder();
10-
//builder.RegisterType<Klass>();
11-
builder.AutoRegisterTypes().AsSelf();
12-
var container = builder.Build();
13-
container.Resolve<Klass>();
149
unsafe
1510
{
16-
var h = IAnyHandler.CreateConsumeDispatcher(container);
17-
h(container, "Hello");
18-
}
11+
var builder = new ContainerBuilder();
12+
//builder.RegisterType<Klass>();
13+
builder.AutoRegisterTypes().AsSelf();
14+
var container = builder.Build();
15+
container.Resolve<Klass>();
16+
unsafe
17+
{
18+
var h = IAnyHandler.CreateConsumeDispatcher(container);
19+
h(container, "Hello");
20+
}
1921

20-
unsafe
21-
{
22-
ReflectionMetadata.RegisterCollection(new()
22+
unsafe
2323
{
24-
Type = typeof(Dictionary<int, string>),
25-
ElementKeyType = typeof(int),
26-
ElementValueType = typeof(string),
27-
Creator = &Create1,
28-
AdderKeyValue = &Add1
29-
});
30-
}
24+
ReflectionMetadata.RegisterCollection(new()
25+
{
26+
Type = typeof(Dictionary<int, string>),
27+
ElementKeyType = typeof(int),
28+
ElementValueType = typeof(string),
29+
Creator = &Create1,
30+
AdderKeyValue = &Add1
31+
});
32+
}
3133

32-
static object Create1(uint capacity)
33-
{
34-
return new Dictionary<int, string>((int)capacity);
34+
MyCtx myCtx = default;
35+
AllocateValueType(ref Unsafe.As<MyCtx, byte>(ref myCtx), ref myCtx.Ptr, &WorkWithAllocatedValueType);
36+
37+
static object Create1(uint capacity)
38+
{
39+
return new Dictionary<int, string>((int)capacity);
40+
}
41+
42+
static void Add1(object c, ref byte key, ref byte value)
43+
{
44+
Unsafe.As<Dictionary<int, string>>(c).Add(Unsafe.As<byte, int>(ref key), Unsafe.As<byte, string>(ref value));
45+
}
46+
47+
static void AllocateValueType(ref byte ctx, ref nint ptr, delegate*<ref byte, void> chain)
48+
{
49+
(int, string) value;
50+
#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
51+
ptr = (nint)(&value);
52+
#pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
53+
chain(ref ctx);
54+
ptr = 0;
55+
}
56+
57+
static void WorkWithAllocatedValueType(ref byte ctx)
58+
{
59+
#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
60+
ref var value = ref *((int, string)*)Unsafe.As<byte, MyCtx>(ref ctx).Ptr;
61+
#pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
62+
value = (42, "Hello");
63+
Console.WriteLine(value);
64+
}
3565
}
3666

37-
static void Add1(object c, ref byte key, ref byte value)
67+
public struct MyCtx
3868
{
39-
Unsafe.As<Dictionary<int, string>>(c).Add(Unsafe.As<byte, int>(ref key), Unsafe.As<byte, string>(ref value));
69+
public nint Ptr;
4070
}
4171

4272
[Generate]

0 commit comments

Comments
 (0)