Skip to content

Commit 41f3de9

Browse files
Adding rust reflection img
1 parent 7b525d8 commit 41f3de9

2 files changed

Lines changed: 6 additions & 83 deletions

File tree

β€Žcppcon2025/cppcon_2025_slides.mdβ€Ž

Lines changed: 6 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ This manual approach has several problems:
238238
239239
---
240240
241-
# Automate the serialization/deserialization process.
242-
241+
# Goal: Seamless Serialization/Deserialization
243242
244243
<img src="images/tofrom.svg" width="100%">
245244
@@ -294,93 +293,17 @@ Player load_player(const std::string& json_str) {
294293
- **Handles nested structures automatically**
295294
- **Performance tuned by the library**
296295

297-
298-
299-
---
300-
301-
# Python
302-
303-
```python
304-
# Python
305-
import json
306-
json_str = json.dumps(player.__dict__)
307-
player = Player(**json.loads(json_str))
308-
```
309-
310-
<img src="images/python.png" width="10%"/>
311-
312-
313-
---
314-
315-
# Python reflection
316-
317-
```Python
318-
def inspect_object(obj):
319-
print(f"Class name: {obj.__class__.__name__}")
320-
for attr, value in vars(obj).items():
321-
print(f" {attr}: {value}")
322-
```
323-
324-
325-
---
326-
327-
# Go
328-
329-
```Go
330-
jsonData, err := json.MarshalIndent(player, "", " ")
331-
if err != nil {
332-
log.Fatalf("Error during serialization: %v", err)
333-
}
334-
var deserializedPlayer Player
335-
err = json.Unmarshal([]byte(jsonStr), &deserializedPlayer)
336-
```
337-
338-
339-
<img src="images/go.svg" />
340-
341-
342-
---
343-
344-
# Go reflection
345-
346-
- Runtime reflection only
347-
348-
```Go
349-
typ := reflect.TypeOf(obj)
350-
for i := 0; i < typ.NumField(); i++ {
351-
field := typ.Field(i)
352-
}
353-
```
354-
355-
356296
---
357297

358-
# Java and C#
298+
# C#
359299

360300
```C#
361301
string jsonString = JsonSerializer.Serialize(player, options);
362302
Player deserializedPlayer = JsonSerializer.Deserialize<Player>(jsonInput, options);
363303
```
364304

365-
366-
<img src="images/java.png" width="10%"/>
367-
368305
<img src="images/csharp.png" width="10%"/>
369306

370-
371-
---
372-
373-
# Java and C# reflection
374-
375-
- Runtime reflection only.
376-
377-
378-
```java
379-
Class<?> playerClass = Player.class;
380-
Object playerInstance = playerClass.getDeclaredConstructor().newInstance();
381-
Field nameField = playerClass.getDeclaredField("name");
382-
```
383-
384307
---
385308

386309
# Rust (serde)
@@ -394,16 +317,16 @@ let player: Player = serde_json::from_str(&json_str)?;
394317

395318
<img src="images/rust.png" width="10%" />
396319

397-
398320
---
399321

400322
# Rust reflection
401323

402324

403-
- Rust does not have ANY introspection.
325+
- Rust does not have ANY reflection.
404326
- You cannot enumerate the methods of a struct. Either at runtime or at compile-time.
405-
- Rust relies on annotation (serde) followed by re-parsing of the code.
327+
- Serde relies on annotation followed by re-parsing of the code.
406328

329+
<img src="image/rust_reflection.png">
407330

408331
---
409332

@@ -412,7 +335,7 @@ let player: Player = serde_json::from_str(&json_str)?;
412335
| language | runtime reflection | compile-time reflection |
413336
|:---------|:-------------------|:------------------------|
414337
| C++ 26 | πŸ‘Ž | βœ… |
415-
| Go | βœ… | πŸ‘Ž |
338+
| Go | βœ… | πŸ‘Ž |
416339
| Java | βœ… | πŸ‘Ž |
417340
| C# | βœ… | πŸ‘Ž |
418341
| Rust | πŸ‘Ž | πŸ‘Ž |
115 KB
Loading

0 commit comments

Comments
Β (0)