windows-rdl 0.100.0

RDL parser library and ECMA-335 generator
Documentation
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# RDL

> A Rust-like source format for Windows metadata.

- [windows-rdl crate docs]../../../docs/crates/windows-rdl.md
- [Source]https://github.com/microsoft/windows-rs/tree/master/crates/libs/rdl

`windows-rdl` parses RDL (Rust Definition Language), a small Rust-like syntax for Windows APIs. It
emits ECMA-335 `.winmd` metadata for `windows-bindgen`. It also writes canonical RDL from `.winmd`
files.

## Getting started

```rust
#[winrt]
mod Contoso {
    mod Sprockets {
        // Define an interface
        interface ISprocket {
            fn GetStatus(&self) -> SprocketStatus;
            fn Spin(&self, speed: f32);
        }

        // Define a struct
        struct Sprocket {
            TeethCount: u32,
            Diameter: f32,
        }

        // Define an enum with explicit discriminants
        #[repr(i32)]
        enum SprocketStatus {
            Idle = 0,
            Spinning = 1,
            Locked = 2,
            Malfunctioning = 3,
        }
    }
}
```

## RDL syntax

### Basic syntax

#### Comments

```rust
// Single-line comments start with //

/* Multi-line comments
   use C-style syntax */
```

### Attributes

#### `#[winrt]`

Use `#[winrt]` on a module that declares WinRT types. It enables generic interfaces, generic
delegates, and WinRT arrays.

Syntax:

```rust
#[winrt]
mod ModuleName {
    /* ... */
}
```

Example:

```rust
#[winrt]
mod Contoso {
    mod Sprockets {
        /* ... */
    }
}
```

#### `#[win32]`

Use `#[win32]` on a module that declares Win32 types. It enables fixed arrays and unions.

Syntax:

```rust
#[win32]
mod ModuleName {
    /* ... */
}
```

Example:

```rust
#[win32]
mod Contoso {
    mod Sprockets {
        /* ... */
    }
}
```

### Type definitions

#### Modules and namespaces

Modules group types and APIs. A top-level module maps to a metadata namespace.

Syntax:

```rust
mod ModuleName {
    /* ... */
}
```

Modules may be nested.

Example:

```rust
#[winrt]
mod Contoso {
    mod Sprockets {
        struct Sprocket {
            TeethCount: u32,
            Diameter: f32,
        }

        #[repr(i32)]
        enum SprocketStatus {
            Idle = 0,
            Spinning = 1,
            Malfunctioning = 2,
        }

        interface ISprocketFactory {
            fn CreateSprocket(&self, teeth: u32, diameter: f32) -> Sprocket;
            fn GetStatus(&self, s: Sprocket) -> SprocketStatus;
        }
    }
}
```

#### Enums

Enums define named constants.

Syntax:

```rust
#[repr(type)]
enum EnumName {
    Variant1 = value1,
    Variant2 = value2,
}
```

`#[repr(type)]` sets the underlying integer type. Supported types are `i8`, `u8`, `i16`, `u16`,
`i32`, `u32`, `i64`, and `u64`.

Example:

```rust
#[repr(i32)]
enum SprocketStatus {
    Idle = 0,
    Spinning = 1,
    Locked = 2,
    Malfunctioning = 3,
}
```

#### Structs

Structs define data types with named fields.

Syntax:

```rust
struct StructName {
    FieldName: Type,
    // ...
}
```

Example:

```rust
struct Sprocket {
    TeethCount: u32,
    Diameter: f32,
}
```

Fixed-size arrays in Win32:

In a `#[win32]` module, structs can include fixed-size arrays. Use them for buffers and other
fixed-layout data.

Syntax:

```rust
struct StructName {
    FieldName: [type; size],
    // ...
}
```

Example:

```rust
#[win32]
mod Contoso {
    mod Sprockets {
        struct SprocketDataBuffer {
            HeaderSize: u32,
            Data: [u8; 256],
        }
    }
}
```

#### Unions

Unions define fields that share one memory location. All fields start at offset zero.

Syntax:

```rust
union UnionName {
    FieldName: Type,
}
```

Example:

```rust
union SprocketHandle {
    AsInt: i32,
    AsFloat: f32,
    AsBytes: [u8; 4],
}
```

#### Interfaces

Interfaces define method contracts.

Syntax:

```rust
interface InterfaceName {
    fn MethodName(&self, Parameter: Type) -> ReturnType;
}
```

Methods use `fn` and require `&self` as the first parameter. Use `->` for a return type. Omit it for
void.

Example:

```rust
interface ISprocket {
    fn GetStatus(&self) -> SprocketStatus;
    fn Spin(&self, speed: f32);
    fn Stop(&self);
}
```

Generic interfaces in WinRT:

In a `#[winrt]` module, interfaces can have type parameters. WinRT collections and event handlers
use them.

Syntax:

```rust
interface InterfaceName<Type1, Type2> {
    fn MethodName(&self, Parameter: Type1) -> Type2;
}
```

Example:

```rust
#[winrt]
mod Contoso {
    mod Sprockets {
        interface ICatalog<K, V> {
            fn Get(&self, key: K) -> V;
            fn Insert(&self, key: K, value: V);
            fn Remove(&self, key: K) -> bool;
        }
    }
}
```

#### Delegates

Delegates define callable types with method-like semantics.

Syntax:

```rust
delegate fn DelegateName(Parameter: Type) -> ReturnType;
```

Example:

```rust
#[winrt]
mod Contoso {
    mod Sprockets {
        delegate fn SprocketStatusChanged(status: SprocketStatus);
    }
}
```

Generic delegates in WinRT:

In a `#[winrt]` module, delegates can also have type parameters.

Syntax:

```rust
delegate fn DelegateName<Type1, ...>(Parameter: Type1, ...) -> ReturnType;
```

Example:

```rust
#[winrt]
mod Contoso {
    mod Sprockets {
        delegate fn SprocketEventHandler<T>(sender: IObject, event: T);
    }
}
```

#### Classes (WinRT)

```rust
class ClassName : BaseClassName {
    ImplementedInterface1,
    ImplementedInterface2
}
```

Classes can extend a base class. The first interface in the list is the default interface.

Example:

```rust
#[winrt]
mod Contoso {
    mod Sprockets {
        class Sprocket {
            ISprocket,
        }

        #[activatable(1)]
        class ActivatableSprocket: Sprocket {
            ISprocket,
            #[activatable(1)]
            ISprocketFactory,
            #[statics(1)]
            ISprocketStatics,
        }

        interface ISprocket {}
        interface ISprocketFactory {}
        interface ISprocketStatics {}
    }
}
```

`#[activatable(...)]` on a class enables default construction. `#[activatable(...)]` and
`#[statics(...)]` on interfaces mark factory and statics interfaces.

#### Attributes

Attributes define metadata annotations. Each constructor uses `fn` and a parameter list.

Syntax:

```rust
attribute AttributeName {
    fn(Parameter: Type, ...);
}
```

Example:

```rust
#[winrt]
mod Contoso {
    mod Sprockets {
        attribute Discontinued {
            fn(version: u32);
            fn(version: u32, note: String);
        }
    }
}
```

#### Constants

Constants define named primitive values or `GUID` values.

Syntax:

```rust
const Name: Type = value;
```

Example:

```rust
#[win32]
mod Contoso {
    mod Sprockets {
        const MAX_TEETH: u32 = 256;
        const MIN_TEETH: u32 = 1;
        const SPROCKET_MACHINE_ID: GUID = 0xe436ebb1_524f_11ce_9f53_0020af0ba770;
    }
}
```

#### Functions

Functions declare external signatures from another library. The `#[link]` attribute sets the library
name and ABI.

Syntax:

```rust
#[link(name = "library", abi = "[system|C]")]
fn FunctionName(Parameter: Type, ...) -> ReturnType;
```

`#[link(name = "...")]` names the library that provides the API. `#[link(abi = "...")]` sets the
ABI: `"system"` or `"C"`.

Example:

```rust
#[win32]
mod Contoso {
    mod Sprockets {
        mod Platform {
            #[link(name = "sprockets.dll", abi = "system")]
            fn InitializeSprocketFactory();
            
            #[link(name = "sprockets.dll", abi = "C")]
            fn GetSprocketCount() -> u32;
        }
    }
}
```

#### Parameter direction attributes

Parameters can carry direction and optional attributes that map to Win32 SAL annotations. These
attributes control generated binding behavior.

| Attribute | Meaning | Corresponding SAL |
|-----------|---------|-------------------|
| `#[in]` | Input parameter. Data flows into the function. | `_In_`, `_In_z_`, and related forms |
| `#[out]` | Output parameter. Data flows out of the function. | `_Out_`, `_Out_z_`, and related forms |
| `#[opt]` | Parameter is optional and can be `NULL`. | `_In_opt_`, `_Out_opt_`, `_Inout_opt_`, and related forms |

When neither `#[in]` nor `#[out]` is set, the reader infers direction from the type: mutable
pointers and references (`*mut T`, `&mut T`) default to `#[out]`. Everything else defaults to
`#[in]`. Use explicit attributes only when the SAL annotation differs from the inferred direction.

When `#[in]` and `#[out]` appear on one parameter, they map to `_Inout_`. The parameter is both
input and output.

The formatter writes `#[in]`. The reader also accepts the Rust raw-identifier spelling
`#[r#in]`.

RDL cannot spell a `Param` row with neither the In nor Out flag. Omitting both attributes invokes
the type-based default instead. A metadata row with neither flag therefore reads back as In.

Example:

```rust
#[win32]
mod Windows {
    mod Win32 {
        mod Api {
            #[library("example.dll")]
            extern fn ReadBuffer(
                #[in] data: *mut i32,    // _In_ - input despite mutable pointer
                count: i32,              // plain In (inferred)
            );

            #[library("example.dll")]
            extern fn Transform(
                #[in] #[out] value: *mut i32,  // _Inout_
            );

            #[library("example.dll")]
            extern fn LookupByName(
                #[opt] name: *const i8,  // _In_opt_
            );
        }
    }
}
```

When `windows-clang` parses Windows SDK headers, it extracts SAL annotations automatically. It emits
the matching direction attributes in generated RDL. Supported SAL macros include `_In_`, `_Out_`,
`_Inout_`, `_In_opt_`, `_Out_opt_`, `_Inout_opt_`, `_Outptr_`, `_COM_Outptr_`, `_In_reads_`,
`_Out_writes_`, and their opt, z, and bytes variants.

Raw pointer chains must use one constness at every depth. `*mut *mut T` and
`*const *const T` are supported. Mixed chains such as `*mut *const T` are rejected because the
metadata type model stores one constness bit for the whole pointer depth. The same check applies
when a pointer chain appears inside a reference.

`#[len_param(N)]` and `#[size_param(N)]` record a raw zero-based parameter position. They do not
refer to a parameter by name. If parameters are reordered, these values must be updated by hand.
`#[len_const(N)]` records a constant element count and has no parameter relationship.

### Array types

#### WinRT arrays

WinRT arrays are dynamic arrays managed by the runtime. RDL supports three forms:

1. Input arrays - passed as read-only input.
2. Output arrays - allocated by the callee and freed by the caller with `CoTaskMemFree`.
3. Return arrays - returned from methods and managed with `CoTaskMemFree`.

Example:

```rust
#[winrt]
mod Contoso {
    mod Sprockets {
        interface ISprocketInventory {
            // Return an array of sprocket IDs
            fn GetAllIds(&self) -> [u32];
            
            // Input array - filter by sizes
            fn FilterBySize(&self, sizes: [f32]) -> u32;
            
            // Output array - collect names
            fn CollectNames(&self, output: &mut [String]);
            
            // Combined input/output
            fn Transform(&self, input: [u32], output: &mut [Sprocket]);
        }
    }
}
```

WinRT arrays only work in `#[winrt]` modules. Use fixed-size arrays for `#[win32]` modules.

#### Fixed-size arrays

Fixed-size arrays have a compile-time size. They can appear in struct fields and method parameters
in `#[win32]` modules.

Example:

```rust
#[win32]
mod Contoso {
    mod Sprockets {
        struct SprocketHeader {
            Signature: [u8; 4],
            Version: u32,
            Reserved: [u8; 24],
        }
        
        interface ISprocketBuffer {
            fn Read(&self, buffer: [u8; 256]) -> u32;
            fn Write(&self, buffer: &mut [u8; 256]) -> u32;
        }
    }
}
```

WinRT does not support fixed arrays.

### Built-in types

| RDL Type   | Description                        |
|------------|------------------------------------|
| `i8`       | 8-bit signed integer               |
| `u8`       | 8-bit unsigned integer             |
| `i16`      | 16-bit signed integer              |
| `u16`      | 16-bit unsigned integer            |
| `i32`      | 32-bit signed integer              |
| `u32`      | 32-bit unsigned integer            |
| `i64`      | 64-bit signed integer              |
| `u64`      | 64-bit unsigned integer            |
| `f32`      | 32-bit float                       |
| `f64`      | 64-bit float                       |
| `isize`    | Pointer-sized signed integer       |
| `usize`    | Pointer-sized unsigned integer     |
| `bool`     | Boolean                            |
| `String`   | String (HSTRING)                   |
| `GUID`     | Globally unique identifier         |
| `HRESULT`  | Windows error code                 |

### Pointer and reference types

| RDL Type     | Description                   |
|--------------|-------------------------------|
| `*mut T`     | Mutable raw pointer to T      |
| `*const T`   | Const raw pointer to T        |
| `&mut T`     | Mutable reference to T        |
| `&T`         | Const reference to T          |