-
Notifications
You must be signed in to change notification settings - Fork 100
/
lifetimes.html
913 lines (458 loc) · 36.4 KB
/
lifetimes.html
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
<!DOCTYPE HTML>
<html lang="en" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Deserializer lifetimes · Serde</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-codeblock-rust/block.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-checklist/checklist.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-anchors/plugin.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<link rel="stylesheet" href="styles/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="examples.html" />
<link rel="prev" href="impl-deserializer.html" />
</head>
<body>
<div class="book with-summary font-size-2">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="./">
<a href="./">
Overview
</a>
</li>
<li class="chapter " data-level="1.2" data-path="help.html">
<a href="help.html">
Help
</a>
</li>
<li class="chapter " data-level="1.3" data-path="data-model.html">
<a href="data-model.html">
Serde data model
</a>
</li>
<li class="chapter " data-level="1.4" data-path="derive.html">
<a href="derive.html">
Using derive
</a>
</li>
<li class="chapter " data-level="1.5" data-path="attributes.html">
<a href="attributes.html">
Attributes
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="container-attrs.html">
<a href="container-attrs.html">
Container attributes
</a>
</li>
<li class="chapter " data-level="1.5.2" data-path="variant-attrs.html">
<a href="variant-attrs.html">
Variant attributes
</a>
</li>
<li class="chapter " data-level="1.5.3" data-path="field-attrs.html">
<a href="field-attrs.html">
Field attributes
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.6" data-path="custom-serialization.html">
<a href="custom-serialization.html">
Custom serialization
</a>
<ul class="articles">
<li class="chapter " data-level="1.6.1" data-path="impl-serialize.html">
<a href="impl-serialize.html">
Implementing Serialize
</a>
</li>
<li class="chapter " data-level="1.6.2" data-path="impl-deserialize.html">
<a href="impl-deserialize.html">
Implementing Deserialize
</a>
</li>
<li class="chapter " data-level="1.6.3" data-path="unit-testing.html">
<a href="unit-testing.html">
Unit testing
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.7" data-path="data-format.html">
<a href="data-format.html">
Writing a data format
</a>
<ul class="articles">
<li class="chapter " data-level="1.7.1" data-path="conventions.html">
<a href="conventions.html">
Conventions
</a>
</li>
<li class="chapter " data-level="1.7.2" data-path="error-handling.html">
<a href="error-handling.html">
Error handling
</a>
</li>
<li class="chapter " data-level="1.7.3" data-path="impl-serializer.html">
<a href="impl-serializer.html">
Implementing a Serializer
</a>
</li>
<li class="chapter " data-level="1.7.4" data-path="impl-deserializer.html">
<a href="impl-deserializer.html">
Implementing a Deserializer
</a>
</li>
</ul>
</li>
<li class="chapter active" data-level="1.8" data-path="lifetimes.html">
<a href="lifetimes.html">
Deserializer lifetimes
</a>
</li>
<li class="chapter " data-level="1.9" data-path="examples.html">
<a href="examples.html">
Examples
</a>
<ul class="articles">
<li class="chapter " data-level="1.9.1" data-path="json.html">
<a href="json.html">
Structs and enums in JSON
</a>
</li>
<li class="chapter " data-level="1.9.2" data-path="enum-representations.html">
<a href="enum-representations.html">
Enum representations
</a>
</li>
<li class="chapter " data-level="1.9.3" data-path="attr-default.html">
<a href="attr-default.html">
Default value for a field
</a>
</li>
<li class="chapter " data-level="1.9.4" data-path="attr-flatten.html">
<a href="attr-flatten.html">
Struct flattening
</a>
</li>
<li class="chapter " data-level="1.9.5" data-path="attr-bound.html">
<a href="attr-bound.html">
Handwritten generic type bounds
</a>
</li>
<li class="chapter " data-level="1.9.6" data-path="deserialize-map.html">
<a href="deserialize-map.html">
Deserialize for custom map type
</a>
</li>
<li class="chapter " data-level="1.9.7" data-path="stream-array.html">
<a href="stream-array.html">
Array of values without buffering
</a>
</li>
<li class="chapter " data-level="1.9.8" data-path="enum-number.html">
<a href="enum-number.html">
Serialize enum as number
</a>
</li>
<li class="chapter " data-level="1.9.9" data-path="attr-rename.html">
<a href="attr-rename.html">
Serialize fields as camelCase
</a>
</li>
<li class="chapter " data-level="1.9.10" data-path="attr-skip-serializing.html">
<a href="attr-skip-serializing.html">
Skip serializing field
</a>
</li>
<li class="chapter " data-level="1.9.11" data-path="remote-derive.html">
<a href="remote-derive.html">
Derive for remote crate
</a>
</li>
<li class="chapter " data-level="1.9.12" data-path="deserialize-struct.html">
<a href="deserialize-struct.html">
Manually deserialize struct
</a>
</li>
<li class="chapter " data-level="1.9.13" data-path="ignored-any.html">
<a href="ignored-any.html">
Discarding data
</a>
</li>
<li class="chapter " data-level="1.9.14" data-path="transcode.html">
<a href="transcode.html">
Transcode into another format
</a>
</li>
<li class="chapter " data-level="1.9.15" data-path="string-or-struct.html">
<a href="string-or-struct.html">
Either string or struct
</a>
</li>
<li class="chapter " data-level="1.9.16" data-path="convert-error.html">
<a href="convert-error.html">
Convert error types
</a>
</li>
<li class="chapter " data-level="1.9.17" data-path="custom-date-format.html">
<a href="custom-date-format.html">
Custom date format
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.10" data-path="no-std.html">
<a href="no-std.html">
No-std support
</a>
</li>
<li class="chapter " data-level="1.11" data-path="feature-flags.html">
<a href="feature-flags.html">
Feature flags
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." >Deserializer lifetimes</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal markdown-section">
<h1 id="understanding-deserializer-lifetimes"><a name="understanding-deserializer-lifetimes" class="plugin-anchor" href="#understanding-deserializer-lifetimes"><i class="fa fa-link" aria-hidden="true"></i></a>Understanding deserializer lifetimes</h1>
<p>The <a href="https://docs.rs/serde/1/serde/trait.Deserialize.html" target="_blank"><code>Deserialize</code></a> and <a href="https://docs.rs/serde/1/serde/trait.Deserializer.html" target="_blank"><code>Deserializer</code></a> traits both have a lifetime called
<code>'de</code>, as do some of the other deserialization-related traits.</p>
<pre><code class="lang-rust"><span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">Deserialize</span></span><<span class="hljs-symbol">'de</span>>: <span class="hljs-built_in">Sized</span> {
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">deserialize</span></span><D>(deserializer: D) -> <span class="hljs-built_in">Result</span><<span class="hljs-keyword">Self</span>, D::Error>
<span class="hljs-keyword">where</span>
D: Deserializer<<span class="hljs-symbol">'de</span>>;
}
</code></pre>
<p>This lifetime is what enables Serde to safely perform efficient zero-copy
deserialization across a variety of data formats, something that would be
impossible or recklessly unsafe in languages other than Rust.</p>
<pre><code class="lang-rust"><span class="hljs-meta">#[derive(Deserialize)]</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">User</span></span><<span class="hljs-symbol">'a</span>> {
id: <span class="hljs-keyword">u32</span>,
name: &<span class="hljs-symbol">'a</span> <span class="hljs-keyword">str</span>,
screen_name: &<span class="hljs-symbol">'a</span> <span class="hljs-keyword">str</span>,
location: &<span class="hljs-symbol">'a</span> <span class="hljs-keyword">str</span>,
}
</code></pre>
<p>Zero-copy deserialization means deserializing into a data structure, like the
<code>User</code> struct above, that borrows string or byte array data from the string or
byte array holding the input. This avoids allocating memory to store a string
for each individual field and then copying string data out of the input over to
the newly allocated field. Rust guarantees that the input data outlives the
period during which the output data structure is in scope, meaning it is
impossible to have dangling pointer errors as a result of losing the input data
while the output data structure still refers to it.</p>
<h2 id="trait-bounds"><a name="trait-bounds" class="plugin-anchor" href="#trait-bounds"><i class="fa fa-link" aria-hidden="true"></i></a>Trait bounds</h2>
<p>There are two main ways to write <code>Deserialize</code> trait bounds, whether on an impl
block or a function or anywhere else.</p>
<ul>
<li><p><strong><code><'de, T> where T: Deserialize<'de></code></strong></p>
<p> This means "T can be deserialized from <strong>some</strong> lifetime." The caller gets
to decide what lifetime that is. Typically this is used when the caller also
provides the data that is being deserialized from, for example in a function
like <a href="https://docs.rs/serde_json/1/serde_json/fn.from_str.html" target="_blank"><code>serde_json::from_str</code></a>. In that case the input data must also have
lifetime <code>'de</code>, for example it could be <code>&'de str</code>.</p>
</li>
<li><p><strong><code><T> where T: DeserializeOwned</code></strong></p>
<p> This means "T can be deserialized from <strong>any</strong> lifetime." The callee gets to
decide what lifetime. Usually this is because the data that is being
deserialized from is going to be thrown away before the function returns, so
T must not be allowed to borrow from it. For example a function that accepts
base64-encoded data as input, decodes it from base64, deserializes a value
of type T, then throws away the result of base64 decoding. Another common
use of this bound is functions that deserialize from an IO stream, such as
<a href="https://docs.rs/serde_json/1/serde_json/fn.from_reader.html" target="_blank"><code>serde_json::from_reader</code></a>.</p>
<p> To say it more technically, the <a href="https://docs.rs/serde/1/serde/de/trait.DeserializeOwned.html" target="_blank"><code>DeserializeOwned</code></a> trait is equivalent to
the <a href="https://doc.rust-lang.org/nomicon/hrtb.html" target="_blank">higher-rank trait bound</a> <code>for<'de> Deserialize<'de></code>. The only
difference is <code>DeserializeOwned</code> is more intuitive to read. It means T owns
all the data that gets deserialized.</p>
</li>
</ul>
<p>Note that <code><T> where T: Deserialize<'static></code> is never what you want. Also
<code>Deserialize<'de> + 'static</code> is never what you want. Generally writing <code>'static</code>
anywhere near <code>Deserialize</code> is a sign of being on the wrong track. Use one of
the above bounds instead.</p>
<h2 id="transient-borrowed-and-owned-data"><a name="transient-borrowed-and-owned-data" class="plugin-anchor" href="#transient-borrowed-and-owned-data"><i class="fa fa-link" aria-hidden="true"></i></a>Transient, borrowed, and owned data</h2>
<p>The Serde data model has three flavors of strings and byte arrays during
deserialization. They correspond to different methods on the <a href="https://docs.rs/serde/1/serde/de/trait.Visitor.html" target="_blank"><code>Visitor</code></a> trait.</p>
<ul>
<li><strong>Transient</strong> — <a href="https://docs.rs/serde/1/serde/de/trait.Visitor.html#method.visit_str" target="_blank"><code>visit_str</code></a> accepts a <code>&str</code>.</li>
<li><strong>Borrowed</strong> — <a href="https://docs.rs/serde/1/serde/de/trait.Visitor.html#method.visit_borrowed_str" target="_blank"><code>visit_borrowed_str</code></a> accepts a <code>&'de str</code>.</li>
<li><strong>Owned</strong> — <a href="https://docs.rs/serde/1/serde/de/trait.Visitor.html#method.visit_string" target="_blank"><code>visit_string</code></a> accepts a <code>String</code>.</li>
</ul>
<p>Transient data is not guaranteed to last beyond the method call it is passed to.
Often this is sufficient, for example when deserializing something like an IP
address from a Serde string using the <a href="https://doc.rust-lang.org/std/str/trait.FromStr.html" target="_blank"><code>FromStr</code></a> trait. When it is not
sufficient, the data can be copied by calling <a href="https://doc.rust-lang.org/std/borrow/trait.ToOwned.html" target="_blank"><code>to_owned()</code></a>. Deserializers
commonly use transient data when input from an IO stream is being buffered in
memory before being passed to the <code>Visitor</code>, or when escape sequences are being
processed so the resulting string is not present verbatim in the input.</p>
<p>Borrowed data is guaranteed to live at least as long as the <code>'de</code> lifetime
parameter of the <code>Deserializer</code>. Not all deserializers support handing out
borrowed data. For example when deserializing from an IO stream no data can be
borrowed.</p>
<p>Owned data is guaranteed to live as long as the <a href="https://docs.rs/serde/1/serde/de/trait.Visitor.html" target="_blank"><code>Visitor</code></a> wants it to. Some
visitors benefit from receiving owned data. For example the <code>Deserialize</code> impl
for Rust's <code>String</code> type benefits from being given ownership of the Serde string
data that has been deserialized.</p>
<h2 id="the-deserializede-lifetime"><a name="the-deserializede-lifetime" class="plugin-anchor" href="#the-deserializede-lifetime"><i class="fa fa-link" aria-hidden="true"></i></a>The Deserialize<'de> lifetime</h2>
<p>This lifetime records the constraints on how long data borrowed by this type
must be valid.</p>
<p>Every lifetime of data borrowed by this type must be a bound on the <code>'de</code>
lifetime of its <code>Deserialize</code> impl. If this type borrows data with lifetime
<code>'a</code>, then <code>'de</code> must be constrained to outlive <code>'a</code>.</p>
<pre><code class="lang-rust"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">S</span></span><<span class="hljs-symbol">'a</span>, <span class="hljs-symbol">'b</span>, T> {
a: &<span class="hljs-symbol">'a</span> <span class="hljs-keyword">str</span>,
b: &<span class="hljs-symbol">'b</span> <span class="hljs-keyword">str</span>,
bb: &<span class="hljs-symbol">'b</span> <span class="hljs-keyword">str</span>,
t: T,
}
<span class="hljs-keyword">impl</span><<span class="hljs-symbol">'de</span>: <span class="hljs-symbol">'a</span> + <span class="hljs-symbol">'b</span>, <span class="hljs-symbol">'a</span>, <span class="hljs-symbol">'b</span>, T> Deserialize<<span class="hljs-symbol">'de</span>> <span class="hljs-keyword">for</span> S<<span class="hljs-symbol">'a</span>, <span class="hljs-symbol">'b</span>, T>
<span class="hljs-keyword">where</span>
T: Deserialize<<span class="hljs-symbol">'de</span>>,
{
<span class="hljs-comment">/* ... */</span>
}
</code></pre>
<p>If this type does not borrow any data from the <code>Deserializer</code>, there are simply
no bounds on the <code>'de</code> lifetime. Such types automatically implement the
<a href="https://docs.rs/serde/1/serde/de/trait.DeserializeOwned.html" target="_blank"><code>DeserializeOwned</code></a> trait.</p>
<pre><code class="lang-rust"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">S</span></span> {
owned: <span class="hljs-built_in">String</span>,
}
<span class="hljs-keyword">impl</span><<span class="hljs-symbol">'de</span>> Deserialize<<span class="hljs-symbol">'de</span>> <span class="hljs-keyword">for</span> S {
<span class="hljs-comment">/* ... */</span>
}
</code></pre>
<p>The <code>'de</code> lifetime <strong>should not</strong> appear in the type to which the <code>Deserialize</code>
impl applies.</p>
<pre><code class="lang-diff"><span class="hljs-deletion">- // Do not do this. Sooner or later you will be sad.</span>
<span class="hljs-deletion">- impl<'de> Deserialize<'de> for Q<'de> {</span>
<span class="hljs-addition">+ // Do this instead.</span>
<span class="hljs-addition">+ impl<'de: 'a, 'a> Deserialize<'de> for Q<'a> {</span>
</code></pre>
<h2 id="the-deserializerde-lifetime"><a name="the-deserializerde-lifetime" class="plugin-anchor" href="#the-deserializerde-lifetime"><i class="fa fa-link" aria-hidden="true"></i></a>The Deserializer<'de> lifetime</h2>
<p>This is the lifetime of data that can be borrowed from the <code>Deserializer</code>.</p>
<pre><code class="lang-rust"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">MyDeserializer</span></span><<span class="hljs-symbol">'de</span>> {
input_data: &<span class="hljs-symbol">'de</span> [<span class="hljs-keyword">u8</span>],
pos: usize,
}
<span class="hljs-keyword">impl</span><<span class="hljs-symbol">'de</span>> Deserializer<<span class="hljs-symbol">'de</span>> <span class="hljs-keyword">for</span> MyDeserializer<<span class="hljs-symbol">'de</span>> {
<span class="hljs-comment">/* ... */</span>
}
</code></pre>
<p>If the <code>Deserializer</code> never invokes <a href="https://docs.rs/serde/1/serde/de/trait.Visitor.html#method.visit_borrowed_str" target="_blank"><code>visit_borrowed_str</code></a> or
<a href="https://docs.rs/serde/1/serde/de/trait.Visitor.html#method.visit_borrowed_bytes" target="_blank"><code>visit_borrowed_bytes</code></a>, the <code>'de</code> lifetime will be an unconstrained lifetime
parameter.</p>
<pre><code class="lang-rust"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">MyDeserializer</span></span><R> {
read: R,
}
<span class="hljs-keyword">impl</span><<span class="hljs-symbol">'de</span>, R> Deserializer<<span class="hljs-symbol">'de</span>> <span class="hljs-keyword">for</span> MyDeserializer<R>
<span class="hljs-keyword">where</span>
R: io::Read,
{
<span class="hljs-comment">/* ... */</span>
}
</code></pre>
<h2 id="borrowing-data-in-a-derived-impl"><a name="borrowing-data-in-a-derived-impl" class="plugin-anchor" href="#borrowing-data-in-a-derived-impl"><i class="fa fa-link" aria-hidden="true"></i></a>Borrowing data in a derived impl</h2>
<p>Fields of type <code>&str</code> and <code>&[u8]</code> are implicitly borrowed from the input data by
Serde. Any other type of field can opt in to borrowing by using the
<code>#[serde(borrow)]</code> attribute.</p>
<pre><code class="lang-rust"><span class="hljs-keyword">use</span> serde::Deserialize;
<span class="hljs-keyword">use</span> std::borrow::Cow;
<span class="hljs-meta">#[derive(Deserialize)]</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Inner</span></span><<span class="hljs-symbol">'a</span>, <span class="hljs-symbol">'b</span>> {
<span class="hljs-comment">// &str and &[u8] are implicitly borrowed.</span>
username: &<span class="hljs-symbol">'a</span> <span class="hljs-keyword">str</span>,
<span class="hljs-comment">// Other types must be borrowed explicitly.</span>
<span class="hljs-meta">#[serde(borrow)]</span>
comment: Cow<<span class="hljs-symbol">'b</span>, <span class="hljs-keyword">str</span>>,
}
<span class="hljs-meta">#[derive(Deserialize)]</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Outer</span></span><<span class="hljs-symbol">'a</span>, <span class="hljs-symbol">'b</span>, <span class="hljs-symbol">'c</span>> {
owned: <span class="hljs-built_in">String</span>,
<span class="hljs-meta">#[serde(borrow)]</span>
inner: Inner<<span class="hljs-symbol">'a</span>, <span class="hljs-symbol">'b</span>>,
<span class="hljs-comment">// This field is never borrowed.</span>
not_borrowed: Cow<<span class="hljs-symbol">'c</span>, <span class="hljs-keyword">str</span>>,
}
</code></pre>
<p>This attribute works by placing bounds on the <code>'de</code> lifetime of the generated
<code>Deserialize</code> impl. For example the impl for the struct <code>Outer</code> defined above
looks like this:</p>
<pre><code class="lang-rust"><span class="hljs-comment">// The lifetimes 'a and 'b are borrowed while 'c is not.</span>
<span class="hljs-keyword">impl</span><<span class="hljs-symbol">'de</span>: <span class="hljs-symbol">'a</span> + <span class="hljs-symbol">'b</span>, <span class="hljs-symbol">'a</span>, <span class="hljs-symbol">'b</span>, <span class="hljs-symbol">'c</span>> Deserialize<<span class="hljs-symbol">'de</span>> <span class="hljs-keyword">for</span> Outer<<span class="hljs-symbol">'a</span>, <span class="hljs-symbol">'b</span>, <span class="hljs-symbol">'c</span>> {
<span class="hljs-comment">/* ... */</span>
}
</code></pre>
<p>The attribute may specify explicitly which lifetimes should be borrowed.</p>
<pre><code class="lang-rust"><span class="hljs-keyword">use</span> std::marker::PhantomData;
<span class="hljs-comment">// This struct borrows the first two lifetimes but not the third.</span>
<span class="hljs-meta">#[derive(Deserialize)]</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Three</span></span><<span class="hljs-symbol">'a</span>, <span class="hljs-symbol">'b</span>, <span class="hljs-symbol">'c</span>> {
a: &<span class="hljs-symbol">'a</span> <span class="hljs-keyword">str</span>,
b: &<span class="hljs-symbol">'b</span> <span class="hljs-keyword">str</span>,
c: PhantomData<&<span class="hljs-symbol">'c</span> <span class="hljs-keyword">str</span>>,
}
<span class="hljs-meta">#[derive(Deserialize)]</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Example</span></span><<span class="hljs-symbol">'a</span>, <span class="hljs-symbol">'b</span>, <span class="hljs-symbol">'c</span>> {
<span class="hljs-comment">// Borrow 'a and 'b only, not 'c.</span>
<span class="hljs-meta">#[serde(borrow = <span class="hljs-meta-string">"'a + 'b"</span>)]</span>
three: Three<<span class="hljs-symbol">'a</span>, <span class="hljs-symbol">'b</span>, <span class="hljs-symbol">'c</span>>,
}
</code></pre>
</section>
</div>
</div>
</div>
<a href="impl-deserializer.html" class="navigation navigation-prev " aria-label="Previous page: Implementing a Deserializer">
<i class="fa fa-angle-left"></i>
</a>
<a href="examples.html" class="navigation navigation-next " aria-label="Next page: Examples">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Deserializer lifetimes","level":"1.8","depth":1,"next":{"title":"Examples","level":"1.9","depth":1,"path":"examples.md","ref":"examples.md","articles":[{"title":"Structs and enums in JSON","level":"1.9.1","depth":2,"path":"json.md","ref":"json.md","articles":[]},{"title":"Enum representations","level":"1.9.2","depth":2,"path":"enum-representations.md","ref":"enum-representations.md","articles":[]},{"title":"Default value for a field","level":"1.9.3","depth":2,"path":"attr-default.md","ref":"attr-default.md","articles":[]},{"title":"Struct flattening","level":"1.9.4","depth":2,"path":"attr-flatten.md","ref":"attr-flatten.md","articles":[]},{"title":"Handwritten generic type bounds","level":"1.9.5","depth":2,"path":"attr-bound.md","ref":"attr-bound.md","articles":[]},{"title":"Deserialize for custom map type","level":"1.9.6","depth":2,"path":"deserialize-map.md","ref":"deserialize-map.md","articles":[]},{"title":"Array of values without buffering","level":"1.9.7","depth":2,"path":"stream-array.md","ref":"stream-array.md","articles":[]},{"title":"Serialize enum as number","level":"1.9.8","depth":2,"path":"enum-number.md","ref":"enum-number.md","articles":[]},{"title":"Serialize fields as camelCase","level":"1.9.9","depth":2,"path":"attr-rename.md","ref":"attr-rename.md","articles":[]},{"title":"Skip serializing field","level":"1.9.10","depth":2,"path":"attr-skip-serializing.md","ref":"attr-skip-serializing.md","articles":[]},{"title":"Derive for remote crate","level":"1.9.11","depth":2,"path":"remote-derive.md","ref":"remote-derive.md","articles":[]},{"title":"Manually deserialize struct","level":"1.9.12","depth":2,"path":"deserialize-struct.md","ref":"deserialize-struct.md","articles":[]},{"title":"Discarding data","level":"1.9.13","depth":2,"path":"ignored-any.md","ref":"ignored-any.md","articles":[]},{"title":"Transcode into another format","level":"1.9.14","depth":2,"path":"transcode.md","ref":"transcode.md","articles":[]},{"title":"Either string or struct","level":"1.9.15","depth":2,"path":"string-or-struct.md","ref":"string-or-struct.md","articles":[]},{"title":"Convert error types","level":"1.9.16","depth":2,"path":"convert-error.md","ref":"convert-error.md","articles":[]},{"title":"Custom date format","level":"1.9.17","depth":2,"path":"custom-date-format.md","ref":"custom-date-format.md","articles":[]}]},"previous":{"title":"Implementing a Deserializer","level":"1.7.4","depth":2,"path":"impl-deserializer.md","ref":"impl-deserializer.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-search","-sharing","sane-sidebar","codeblock-omit","codeblock-rust","edit-link","ga4","bulk-redirect","checklist","anchors"],"root":"_src","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"checklist":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"codeblock-rust":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"sane-sidebar":{},"codeblock-omit":{},"bulk-redirect":{"redirectsFile":"redirects.json"},"ga4":{"anonymize_ip":true,"cookie_domain":"serde.rs","tag":"G-T5QD1R7FZW"},"edit-link":{"label":"Edit","base":"https://github.com/serde-rs/serde-rs.github.io/edit/master/_src"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Serde","language":"en","gitbook":"*"},"file":{"path":"lifetimes.md","mtime":"2022-12-18T19:59:44.491Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2024-06-25T02:17:24.817Z"},"basePath":".","book":{"language":""}});
});
</script>
</div>
<script src="gitbook/gitbook.js"></script>
<script src="gitbook/theme.js"></script>
<script src="gitbook/gitbook-plugin-sane-sidebar/plugin.js"></script>
<script src="gitbook/gitbook-plugin-edit-link/plugin.js"></script>
<script src="gitbook/gitbook-plugin-ga4/plugin.js"></script>
<script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>