Skip to content

Commit dab8088

Browse files
committed
Move field documentation to new file
1 parent d4d558a commit dab8088

File tree

3 files changed

+39
-39
lines changed

3 files changed

+39
-39
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Name mapping](./call_rust_from_cpp/name_mapping.md)
88
- [Wellknown traits](./call_rust_from_cpp/wellknown_traits.md)
99
- [Layout policy](./call_rust_from_cpp/layout_policy.md)
10+
- [Fields](./call_rust_from_cpp/fields.md)
1011
- [Types with special support](./call_rust_from_cpp/special_types.md)
1112
- [Panic and exceptions](./call_rust_from_cpp/panic_and_exceptions.md)
1213
- [Calling C++ from Rust](./call_cpp_from_rust/index.md)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Fields as underlying types
2+
3+
When you declare fields in a tuple or struct using `field name (offset = X, type = T);`, the generated C++ exposes helper wrapper types:
4+
5+
- `rust::FieldOwned<T, OFFSET>` for fields on owning types
6+
- `rust::FieldRef<T, OFFSET>` for fields on `Ref<Ty>`
7+
- `rust::FieldRefMut<T, OFFSET>` for fields on `RefMut<Ty>`
8+
9+
These wrappers now act as their underlying type `T` in many contexts:
10+
11+
- `Ref<T>` construction from any `Field*<T, OFFSET>`
12+
- Implicit read via `operator T()` and `.read()` for value-like access
13+
- Method calls are forwarded when applicable
14+
15+
Example:
16+
17+
```C++
18+
rust::Tuple<int32_t, rust::std::string::String> t{42, "hi"_rs.to_owned()};
19+
20+
// Read value
21+
int32_t v = t.f0; // operator T() on FieldOwned<int32_t, 0>
22+
23+
// Get a Ref<T> from a field
24+
rust::Ref<int32_t> r = t.f0;
25+
26+
// Access methods through Ref from Field wrappers
27+
rust::Ref<rust::std::string::String> sref = t.f1;
28+
auto len = sref.len();
29+
30+
// From references to container, fields become FieldRef/FieldRefMut
31+
rust::Ref<decltype(t)> rt = t;
32+
auto l1 = rt.f1.len();
33+
34+
rust::RefMut<decltype(t)> mt = t;
35+
mt.f1.push_str("!"_rs);
36+
```
37+
38+
See `examples/regression_test1` for a runnable demonstration.

book/src/call_rust_from_cpp/special_types.md

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,3 @@ tries to support them using C++ feature called User-defined Literals.
1717
| `'a'_rs_b` | `b'a'` | `uint8_t` | Not Implemented | unconditionally |
1818
| `"hello"_rs_b` | `b"hello"` | `rust::Ref<rust::Slice<uint8_t>>` | Not Implemented | `[u8]` |
1919
| `"hello"_rs_c` | `c"hello"` | `rust::Ref<rust::ffi::CStr>` | Not Implemented | `::rust::ffi::CStr` |
20-
21-
## Fields as underlying types
22-
23-
When you declare fields in a tuple or struct using `field name (offset = X, type = T);`, the generated C++ exposes helper wrapper types:
24-
25-
- `rust::FieldOwned<T, OFFSET>` for fields on owning types
26-
- `rust::FieldRef<T, OFFSET>` for fields on `Ref<Ty>`
27-
- `rust::FieldRefMut<T, OFFSET>` for fields on `RefMut<Ty>`
28-
29-
These wrappers now act as their underlying type `T` in many contexts:
30-
31-
- `Ref<T>` construction from any `Field*<T, OFFSET>`
32-
- Implicit read via `operator T()` and `.read()` for value-like access
33-
- Method calls are forwarded when applicable
34-
35-
Example:
36-
37-
```C++
38-
rust::Tuple<int32_t, rust::std::string::String> t{42, "hi"_rs.to_owned()};
39-
40-
// Read value
41-
int32_t v = t.f0; // operator T() on FieldOwned<int32_t, 0>
42-
43-
// Get a Ref<T> from a field
44-
rust::Ref<int32_t> r = t.f0;
45-
46-
// Access methods through Ref from Field wrappers
47-
rust::Ref<rust::std::string::String> sref = t.f1;
48-
auto len = sref.len();
49-
50-
// From references to container, fields become FieldRef/FieldRefMut
51-
rust::Ref<decltype(t)> rt = t;
52-
auto l1 = rt.f1.len();
53-
54-
rust::RefMut<decltype(t)> mt = t;
55-
mt.f1.push_str("!"_rs);
56-
```
57-
58-
See `examples/regression_test1` for a runnable demonstration.

0 commit comments

Comments
 (0)