@@ -1023,7 +1023,7 @@ test TransportOverStdio {
1023
1023
try std .testing .expectEqualStrings ("\" hello from server\" " , message_from_server );
1024
1024
1025
1025
// Client -> Server
1026
- try client_transport .writeJsonMessage ("\" hello from client\" " );
1026
+ try client_transport .any (). writeJsonMessage ("\" hello from client\" " );
1027
1027
1028
1028
// Server <- Client
1029
1029
const message_from_client = try server_transport .readJsonMessage (std .testing .allocator );
@@ -1037,6 +1037,9 @@ test TransportOverStdio {
1037
1037
1038
1038
try std .testing .expectError (error .EndOfStream , server_transport .readJsonMessage (std .testing .allocator ));
1039
1039
try std .testing .expectError (error .EndOfStream , client_transport .readJsonMessage (std .testing .allocator ));
1040
+
1041
+ try std .testing .expectError (error .EndOfStream , server_transport .any ().readJsonMessage (std .testing .allocator ));
1042
+ try std .testing .expectError (error .EndOfStream , client_transport .any ().readJsonMessage (std .testing .allocator ));
1040
1043
}
1041
1044
1042
1045
/// This implementation is totally untested and probably completely wrong. Who is going to use this anyway...
@@ -1102,15 +1105,19 @@ const TransportOverStream = struct {
1102
1105
}
1103
1106
};
1104
1107
1108
+ pub const ThreadSafeTransportConfig = struct {
1109
+ ChildTransport : type ,
1110
+ /// Makes `readJsonMessage` thread-safe.
1111
+ thread_safe_read : bool ,
1112
+ /// Makes `writeJsonMessage` thread-safe.
1113
+ thread_safe_write : bool ,
1114
+ MutexType : ? type ,
1115
+ };
1116
+
1105
1117
/// Wraps a non-thread-safe transport and makes it thread-safe.
1106
- pub fn ThreadSafeTransport (
1107
- comptime ChildTransport : type ,
1108
- comptime thread_safe_read : bool ,
1109
- comptime thread_safe_write : bool ,
1110
- comptime MutexType : ? type ,
1111
- ) type {
1118
+ pub fn ThreadSafeTransport (config : ThreadSafeTransportConfig ) type {
1112
1119
return struct {
1113
- child_transport : ChildTransport ,
1120
+ child_transport : config. ChildTransport ,
1114
1121
in_mutex : @TypeOf (in_mutex_init ) = in_mutex_init ,
1115
1122
out_mutex : @TypeOf (out_mutex_init ) = out_mutex_init ,
1116
1123
@@ -1139,16 +1146,16 @@ pub fn ThreadSafeTransport(
1139
1146
return try transport .child_transport .writeJsonMessage (json_message );
1140
1147
}
1141
1148
1142
- const in_mutex_init = if (MutexType ) | T |
1149
+ const in_mutex_init = if (config . MutexType ) | T |
1143
1150
T {}
1144
- else if (thread_safe_read )
1151
+ else if (config . thread_safe_read )
1145
1152
std.Thread.Mutex {}
1146
1153
else
1147
1154
DummyMutex {};
1148
1155
1149
- const out_mutex_init = if (MutexType ) | T |
1156
+ const out_mutex_init = if (config . MutexType ) | T |
1150
1157
T {}
1151
- else if (thread_safe_write )
1158
+ else if (config . thread_safe_write )
1152
1159
std.Thread.Mutex {}
1153
1160
else
1154
1161
DummyMutex {};
@@ -1160,6 +1167,7 @@ pub fn ThreadSafeTransport(
1160
1167
};
1161
1168
}
1162
1169
1170
+ /// A type-erased Transport.
1163
1171
pub const AnyTransport = struct {
1164
1172
impl : struct {
1165
1173
transport : * anyopaque ,
@@ -1187,7 +1195,7 @@ pub const MethodWithParams = struct {
1187
1195
params : ? std.json.Value ,
1188
1196
};
1189
1197
1190
- pub const MessageOptions = struct {
1198
+ pub const MessageConfig = struct {
1191
1199
/// Must be a tagged union with the following properties:
1192
1200
/// - the field name is the method name of a request message
1193
1201
/// - the field type must be the params type of the method (i.e. `ParamsType(field.name)`)
@@ -1232,8 +1240,8 @@ pub const MessageOptions = struct {
1232
1240
NotificationParams : type ,
1233
1241
};
1234
1242
1235
- pub fn Message (comptime message_options : MessageOptions ) type {
1236
- // TODO validate `message_options .RequestParams` and `message_options .NotificationParams`
1243
+ pub fn Message (comptime config : MessageConfig ) type {
1244
+ // TODO validate `config .RequestParams` and `config .NotificationParams`
1237
1245
// This level of comptime should be illegal...
1238
1246
return union (enum ) {
1239
1247
request : Request ,
@@ -1247,7 +1255,7 @@ pub fn Message(comptime message_options: MessageOptions) type {
1247
1255
id : JsonRPCMessage.ID ,
1248
1256
params : Params ,
1249
1257
1250
- pub const Params = message_options .RequestParams ;
1258
+ pub const Params = config .RequestParams ;
1251
1259
1252
1260
pub const jsonParse = @compileError ("Parsing a Request directly is not implemented! try to parse the Message instead." );
1253
1261
pub const jsonParseFromValue = @compileError ("Parsing a Request directly is not implemented! try to parse the Message instead." );
@@ -1271,7 +1279,7 @@ pub fn Message(comptime message_options: MessageOptions) type {
1271
1279
comptime jsonrpc : []const u8 = "2.0" ,
1272
1280
params : Params ,
1273
1281
1274
- pub const Params = message_options .NotificationParams ;
1282
+ pub const Params = config .NotificationParams ;
1275
1283
1276
1284
pub const jsonParse = @compileError ("Parsing a Notification directly is not implemented! try to parse the Message instead." );
1277
1285
pub const jsonParseFromValue = @compileError ("Parsing a Notification directly is not implemented! try to parse the Message instead." );
0 commit comments