Skip to content

Commit a454955

Browse files
book chapter
1 parent ddf390f commit a454955

3 files changed

Lines changed: 67 additions & 3 deletions

File tree

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [Types with special support](./call_rust_from_cpp/special_types.md)
1212
- [Panic and exceptions](./call_rust_from_cpp/panic_and_exceptions.md)
1313
- [Raw pointers](./call_rust_from_cpp/raw_pointers.md)
14+
- [Template types](./template_types.md)
1415
- [Calling C++ from Rust](./call_cpp_from_rust/index.md)
1516
- [Calling C++ free functions](./call_cpp_from_rust/function.md)
1617
- [Writing `impl` blocks for Rust types in C++](./call_cpp_from_rust/rust_impl.md)

book/src/template_types.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Template types
2+
3+
Template type definitions provide a shorthand syntax to avoid repeating the defintion of methods on common generic types. All of the items within a template type definition are copied into each matching concrete type definition. At the moment, template types are considered unstable and ust be activated with the `#unstable(template_types)` directive.
4+
5+
For example:
6+
```
7+
#unstable(template_types)
8+
9+
type<T> ::std::option::Option<T> {
10+
constructor Some(T);
11+
constructor None;
12+
fn unwrap(self) -> T;
13+
fn is_some(&self) -> bool;
14+
}
15+
16+
type ::std::option::Option<i32> {
17+
#layout(size = 8, align = 8);
18+
wellknown_traits(Copy, Debug);
19+
// the constructors and methods are automatically defined
20+
}
21+
```
22+
23+
You can also define well-known traits and layouts in template types:
24+
```
25+
type<T> [T] {
26+
wellknown_traits(?Sized);
27+
}
28+
29+
type<T> ::std::vec::Vec<T> {
30+
#layout(size = 24, align = 8);
31+
}
32+
33+
type<T> option::Option<&T> {
34+
#layout(size = 8, align = 8);
35+
wellknown_traits(Copy);
36+
}
37+
```
38+
39+
Note that multiple template defintions can apply to the same concrete type, so `Option<&i32>` would inherit definitions from the `Option<T>` template and the `Option<&T>` template.
40+
41+
You can also override the layout given in a template:
42+
```
43+
type<T> Box<T> {
44+
#layout(size = 8, align = 8);
45+
// Most Boxed types are 8 bytes
46+
}
47+
48+
type<T> Box<i32> {} // Inherits the 8 byte layouts
49+
50+
type Box<dyn crate::MyTrait> {
51+
#layout(size = 16, align = 8);
52+
// Boxed trait objects are 16 bytes
53+
}
54+
```
55+
56+
Zngur will still only emit C++ definitions for the concrete types that are defined. Template definitions on their own do not result in any C++ code. This also means that at the moment, any type mentioned in a template definition must be individually defined as well. For example, the following code will fail to compile unless `Option<String>` is defined to make `Result::<String, i64>::ok` valid. If the `Option<T>` template from above is defined, then we must add a definition for `String` as well to support `Option::<String>::unwrap`.
57+
```
58+
type<T, E> ::std::result::Result<T, E> {
59+
fn ok(self) -> ::std::option::Option<T>;
60+
}
61+
62+
type ::std::result::Result<::std::string::String, i64> {
63+
#layout(size = 24, align = 8);
64+
}
65+
```

examples/template_types/main.zng

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ mod crate {
4646
type ::std::vec::Vec<TypeA> {
4747
wellknown_traits(Debug);
4848
}
49-
// These definitions are required for the Vec<T> template to instantiate all methods
50-
type [TypeA] {}
49+
// This definition is required for the Vec<T> template to instantiate all methods
5150
type ::std::option::Option<&TypeA> {}
5251

5352
type TypeB {
@@ -58,7 +57,6 @@ mod crate {
5857

5958
// All relevant definitions are provided by the templates
6059
type ::std::vec::Vec<TypeB> {}
61-
type [TypeB] {}
6260
type ::std::option::Option<&TypeB> {}
6361

6462
type ::std::boxed::Box<dyn MyTrait> {

0 commit comments

Comments
 (0)