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
//! Transitive (across function boundaries) [`ThreadSafety`] inference, mainly for use by frameworks.
//!
//! All methods in this module are always-inlined no-ops, meaning that there is zero runtime cost to them.
#![allow(clippy::inline_always)]
//!
//! **The following is a long explanation that you probably don't have to read.**  
//! In hand-written code, you can always use [`From`] or [`Into`] to cast a [`…<ThreadSafe>`](`ThreadSafe`) type to the matching [`…<ThreadBound>`](`ThreadBound`) type where necessary.  
//! If you receive an opaque type, `use lignin::auto_safety::{AutoSafe as _, Deanonymize as _};` and call `.deanonymize()` on it, then **politely** ask the author to consider being more specific.
//!
//! - - -
//!
//! If you do intend to use this module, please still declare [`ThreadSafe`] explicitly at crate boundaries, or encourage developers using your library to do so.
//! [You can find more information on this near the end of this page.](#limiting-autosafe-exposure)
//!
//! > This feature relies on opaque return types (`-> impl Trait`) leaking [`Send`] and [`Sync`], so the theoretical limit here, even after specialization lands, are four distinct 'real' types with restrictions on conversion incompatibilities.
//! > Fortunately, `lignin` only needs two of these slots with straightforward compatibility, the `!Send + !Sync` and the `Send + Sync` one.
//! >
//! > Please refer to the item documentation for implementation details.
//!
//! # Examples / Usage
//!
//! > All examples share the following definitions:
//! >
//! > ```rust
//! > use lignin::{
//! >   auto_safety::{Align as _, AutoSafe, Deanonymize as _}, // <-- Important!
//! >   Node, ThreadBound, ThreadSafe,
//! > };
//! >
//! > fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! > fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! > fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! > fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! >
//! > fn allocate<'a, T>(value: T) -> &'a T {
//! >   // …
//! > #   Box::leak(Box::new(value))
//! > }
//! > #
//! > # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! > # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! > ```
//! >
//! > I recommend using [`bumpalo`](https://github.com/fitzgen/bumpalo) as VDOM allocator since it is fast and versatile, but `lignin` itself has no preference in this regard.
//!
//! > In all examples and the above, except for those in the [More lenient conversions with `From` and `Into`](#more-lenient-conversions-with-from-and-into) section below, [`Node`] can be replaced by any other [`Vdom`] type.
//!
//! ## Basic Forwarding
//!
//! To mark the [`ThreadSafety`] of a function as inferred, return [`AutoSafe`] wrapping the [`ThreadBound`] version of the VDOM node you want to return.
//!
//! This works with manually-defined sources…:
//!
//! ```rust
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! fn safe_1<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! fn bound_1<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # assert_safe(safe_1().deanonymize());
//! # assert_bound(bound_1().deanonymize());
//! ```
//!
//! …as well as ones where the original return type is inferred (opaque):
//!
//! ```rust
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! fn safe_2<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { inferred_safe() }
//! fn bound_2<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { inferred_bound() }
//! #
//! # assert_safe(safe_2().deanonymize());
//! # assert_bound(bound_2().deanonymize());
//! ```
//!
//! ## Deanonymization
//!
//! Rust doesn't allow consumption of the inferred concrete return type of a function directly, so while the following works fine…:
//!
//! ```rust
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! fn safe_1<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> {
//!   Node::Multi(allocate([safe()]))
//! }
//!
//! fn bound_1<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> {
//!   Node::Multi(allocate([bound()]))
//! }
//! #
//! # assert_safe(safe_1().deanonymize());
//! # assert_bound(bound_1().deanonymize());
//! ```
//!
//! …each of these fails to compile:
//!
//! ```compile_fail
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! fn safe_2<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> {
//!   Node::Ref(allocate(inferred_safe()))
//!   //                 ^^^^^^^^^^^^^^^ expected enum `Node`, found opaque type
//! }
//! ```
//!
//! ```compile_fail
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! fn bound_2<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> {
//!   Node::Ref(allocate(inferred_bound()))
//!   //                 ^^^^^^^^^^^^^^^^ expected enum `Node`, found opaque type
//! }
//! ```
//!
//! ### `.deanonymize()`
//!
//! Call `.deanonymize()` _without qualification_ on an opaquely-typed value to cast it to the underlying named type.
//!
//! This method resolves either through [`AutoSafe`] or [`Deanonymize`], so it's important for both traits to be in scope at the call site!
//!
//! ```
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! fn safe_2<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> {
//!   Node::Multi(allocate([inferred_safe().deanonymize()]))
//! }
//!
//! fn bound_2<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> {
//!   Node::Multi(allocate([inferred_bound().deanonymize()]))
//! }
//! #
//! # assert_safe(safe_2().deanonymize());
//! # assert_bound(bound_2().deanonymize());
//! ```
//!
//! > You also have to do this to annotate the type of local variables…:
//! >
//! > ```
//! > # use lignin::{
//! > #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! > #   Node, ThreadBound, ThreadSafe,
//! > # };
//! > #
//! > # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! > # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! > # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! > # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! > #
//! > # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! > #
//! > # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! > # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! > #
//! > let safe_node: Node<_> = inferred_safe().deanonymize();
//! > let bound_node: Node<_> = inferred_bound().deanonymize();
//! > #
//! > # // No assert here! This test should fail if some some reason this fails to compile without further coercion.
//! > ```
//! >
//! > …or to specify a [`ThreadSafety`] in the return type:
//! >
//! > ```
//! > # use lignin::{
//! > #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! > #   Node, ThreadBound, ThreadSafe,
//! > # };
//! > #
//! > # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! > # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! > # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! > # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! > #
//! > # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! > #
//! > # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! > # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! > #
//! > fn strictly_safe<'a>() -> Node::<'a, ThreadSafe> {
//! >   inferred_safe().deanonymize()
//! > }
//! >
//! > fn strictly_bound<'a>() -> Node::<'a, ThreadBound> {
//! >   inferred_bound().deanonymize()
//! > }
//! > ```
//!
//! #### Identity Cast
//!
//! Calling `.deanonymize()` on named types is valid but ultimately useless, so it produces a warning if resolved like that:
//!
//! ```
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! let safe_node: Node<ThreadSafe> = safe().deanonymize();
//! //                                       ^^^^^^^^^^^
//! let bound_node: Node<ThreadBound> = bound().deanonymize();
//! //                                          ^^^^^^^^^^^
//! //
//! // warning:
//! //   use of deprecated associated function `lignin::auto_safety::<impl lignin::Node<'a, S>>::deanonymize`:
//! //   Call of `.deanonymize()` on named type.
//! ```
//!
//! Macros should suppress this warning as specifically as possible:
//!
//! ```rust
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! {
//!   let rendered = // …
//!   # safe();
//!
//!   #[deny(warnings)]
//!   {
//!     // Use `$crate` if possible, and ideally don't leak these imports into caller code.
//!     use ::lignin::auto_safety::{AutoSafe as _, Deanonymize as _};
//!     #[allow(deprecated)]
//!     rendered.deanonymize()
//!   }
//! }
//! # ;
//! ```
//!
//! #### No Coercion
//!
//! Calls to `.deanonymize()` can't be coerced, so each of the following fails to compile:
//!
//! ```compile_fail
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! let safe_node: Node::<ThreadSafe> = inferred_bound().deanonymize();
//! //             ------------------   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//! //             |                    expected struct `ThreadSafe`, found struct `ThreadBound`
//! //             expected due to this
//! //
//! // note: expected enum `Node<'_, ThreadSafe>`
//! //          found enum `Node<'_, ThreadBound>`
//! ```
//!
//! ```compile_fail
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! let bound_node: Node::<ThreadBound> = inferred_safe().deanonymize();
//! //              -------------------   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//! //              |                     expected struct `ThreadBound`, found struct `ThreadSafe`
//! //              expected due to this
//! //
//! // note: expected enum `Node<'_, ThreadBound>`
//! //          found enum `Node<'_, ThreadSafe>`
//! ```
//!
//! # Alignment
//!
//! The [`Align`] trait behaves a lot like [`Into`], so identity conversions are possible.
//! However, unlike [`Into`], it's implemented so that it can change only a type's [`ThreadSafety`] and isn't warned about by Clippy on identity conversion.
//!
//! This makes it ideal to combine [`Node`] instances with different or unknown [`ThreadSafety`] into a single VDOM:
//!
//! ```rust
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! let safe_to_bound = Node::Multi(allocate([
//!   safe().align(),
//!   bound(),
//! ]));
//! # assert_bound(safe_to_bound);
//!
//! let safe_to_inferred = Node::Multi(allocate([
//!   safe().align(),
//!   inferred_safe().deanonymize(),
//! ]));
//! # assert_safe(safe_to_inferred);
//!
//! let inferred_to_bound = Node::Multi(allocate([
//!   bound(),
//!   inferred_safe().deanonymize().align(),
//! ]));
//! # assert_bound(inferred_to_bound);
//! ```
//!
//! ## More lenient conversions with [`From`] and [`Into`]
//!
//! [`From`] and [`Into`] can both be used to change the [`ThreadSafety`] of [`Vdom`] values from [`ThreadSafe`] to [`ThreadBound`]:
//!
//! ```rust
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! let safe_to_bound: Node<ThreadBound> = safe().into();
//! ```
//!
//! Direct [`Node`] conversions, which can also adjust [`ThreadSafety`], are additionally available for **references** to
//! [`[Node]`](https://doc.rust-lang.org/stable/core/slice/index.html) (into [`Node::Multi`]) and [`str`] (into [`Node::Text`]):
//!
//! ```rust
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! let empty: &[Node<ThreadSafe>] = &[];
//! let empty_node: Node<ThreadSafe> = empty.into();
//!
//! let text_node: Node<ThreadSafe> = "Hello VDOM!".into();
//! ```
//!
//! [`ThreadSafety`] alignment is possible at the same time, but this also means relevant annotations or at least nudges (see below) are often necessary.
//!
//! <!-- TODO: Collect opinions on whether including alignment here is a good idea. -->
//!
//! # [`ThreadSafe`] Preference
//!
//! The Rust compiler can usually infer the correct [`ThreadSafety`] without annotations if valid choices are in any way limited in this regard.
//!
//! However, this isn't the case for most [`Vdom`] expressions without inputs with definite [`ThreadSafety`]…:
//!
//! ```compile_fail
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! let attempt_1 = Node::Multi(&[]);
//! //  ---------   ^^^^^^^^^^^       // See below.
//! ```
//!
//! …or if all inputs are thread-safe and [`.align()`](`Align::align`) is called on each of them:
//!
//! ```compile_fail
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! let attempt_2 = Node::Multi(allocate([safe().align(), inferred_safe().deanonymize().align()]));
//! //  ---------   ^^^^^^^^^^^ cannot infer type for type parameter `S` declared on the enum `Node`
//! //  consider giving `attempt_2` the explicit type `Node<'_, S>`, where the type parameter `S` is specified
//! //
//! // note: cannot satisfy `_: ThreadSafety`
//! // note: required by `Multi`
//! ```
//!
//! In these cases, you can call `.prefer_thread_safe()` on the indeterminate expression to nudge the compiler in the right direction.
//!
//! ```rust
//! # use lignin::{
//! #   auto_safety::{Align as _, AutoSafe, Deanonymize as _},
//! #   Node, ThreadBound, ThreadSafe,
//! # };
//! #
//! # fn safe<'a>() -> Node::<'a, ThreadSafe> { Node::Multi(&[]) }
//! # fn bound<'a>() -> Node::<'a, ThreadBound> { Node::Multi(&[]) }
//! # fn inferred_safe<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { safe() }
//! # fn inferred_bound<'a>() -> impl AutoSafe<Node::<'a, ThreadBound>> { bound() }
//! #
//! # fn allocate<'a, T>(value: T) -> &'a T { Box::leak(Box::new(value)) }
//! #
//! # fn assert_safe<'a>(value: Node<'a, ThreadSafe>) { }
//! # fn assert_bound<'a>(value: Node<'a, ThreadBound>) { }
//! #
//! let safe_1 = Node::Multi(&[]).prefer_thread_safe();
//!
//! let safe_2 = Node::Multi(allocate([
//!   safe().align(),
//!   inferred_safe().deanonymize().align(),
//! ])).prefer_thread_safe();
//! #
//! # assert_safe(safe_1);
//! # assert_safe(safe_2);
//! ```
//!
//! > This is implemented directly on the individual [`Vdom`] type variants, so no additional trait imports are necessary to use it.
//!
//! # Limiting [`AutoSafe`] Exposure
//!
//! Thread-safety inference is powerful, but also dangerous: A change deep in a library could cause a public function return type to shift, breaking compatibility with downstream crates.
//! For this reason, and because of its worse ergonomics, `-> impl AutoSafe<…>` should not be exposed in a crate's public API.
//!
//! A front-end template language or framework author may still want to avoid requiring explicit threading annotations in most cases.
//! Even in that case, it's possible to limit this feature to functions not externally visible, by aliasing it with a generated less visible trait:
//!
//! ```compile_fail
//! # fn main() {} // Don't wrap example in function.
//! use lignin::{
//!   auto_safety::AutoSafe_alias,
//!   Node, ThreadBound,
//! };
//!
//! AutoSafe_alias!(pub(crate) InternalAutoSafe);
//! //-------------------------------------------
//! //`InternalAutoSafe<Node<'static, ThreadBound>>` declared as private
//!
//! pub fn public() -> impl InternalAutoSafe<Node<'static, ThreadBound>> {
//! //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//! //can't leak private trait
//!   Node::Multi(&[]).prefer_thread_safe()
//! }
//! ```
//!
//! ```rust
//! # fn main() {} // Don't wrap example in function.
//! # use lignin::{
//! #   auto_safety::AutoSafe_alias,
//! #   Node, ThreadBound,
//! # };
//! // Same imports.
//!
//! AutoSafe_alias!(pub(crate) InternalAutoSafe);
//!
//! pub(crate) fn less_visible() -> impl InternalAutoSafe<Node<'static, ThreadBound>> {
//!   Node::Multi(&[]).prefer_thread_safe()
//! }
//! ```
//!
//! As the generated trait is a subtrait of [`AutoSafe`], its instances can be treated the same as that trait's,
//! as long as [`AutoSafe`] and [`Deanonymize`] are in scope.

use crate::{
	callback_registry::CallbackSignature, Attribute, CallbackRef, Element, ElementCreationOptions,
	EventBinding, EventBindingOptions, Node, ReorderableFragment, ThreadBound, ThreadSafe,
	ThreadSafety, Vdom,
};

/// Deanonymize towards the general ([`ThreadBound`]) case. Used as `-> impl AutoSafe<…>`.
///
/// See module documentation for usage.
pub trait AutoSafe<BoundVariant>
where
	Self: Vdom + Align<BoundVariant>,
	BoundVariant: Vdom<ThreadSafety = ThreadBound>,
{
	/// Deanonymize towards a compatible concrete type.
	///
	/// This method is by reference, so it will resolve with lower priority than the by-value method on [`Deanonymize`].  
	/// Note that not all tooling will show the correct overload here, but the compiler knows which to pick.
	#[must_use]
	#[inline(always)] // Plain deref.
	fn deanonymize(&self) -> BoundVariant {
		unsafe {
			// SAFETY:
			// Under normal circumstances, this trait or method would have to be `unsafe`.
			// However, we're ensuring only sound implementations exist by sealing it and carefully implementing it only across layout-compatible types.
			*(self as *const Self).cast()
		}
	}
}

/// Deanonymize towards the special ([`ThreadSafe`]) case. **This trait must be in scope for correct inference!**
///
/// See module documentation for usage.
pub trait Deanonymize<SafeVariant>
where
	Self: Vdom + Send + Sync,
	SafeVariant: Vdom<ThreadSafety = ThreadSafe>,
{
	/// Deanonymize towards a compatible concrete type.
	///
	/// This method is by value, so it will resolve with higher priority than the by-reference method on [`AutoSafe`].  
	/// Note that not all tooling will show the correct overload here, but the compiler knows which to pick.
	#[must_use]
	#[inline(always)] // No-op.
	fn deanonymize(self) -> SafeVariant {
		unsafe {
			// SAFETY:
			// Under normal circumstances, this trait or method would have to be `unsafe`.
			// However, we're ensuring only sound implementations exist by sealing it and carefully implementing it only across layout-compatible types.
			*(&self as *const Self).cast()
		}
	}
}

impl<'a, S, T> AutoSafe<T> for S
where
	S: Vdom + Align<T>,
	T: Vdom<ThreadSafety = ThreadBound>,
{
}

/// Contextually thread-binds an instance, or not. Use only without qualification.
///
/// This trait acts as [`Into`] on and between variants of the same [`Vdom`] type, but without raising `useless_conversion` warnings.
///
/// See module documentation for when to use this trait and when it's unnecessary.
pub trait Align<T: Vdom>: Vdom {
	/// Contextually thread-binds an instance, or not. Use only without qualification.
	#[allow(clippy::inline_always)]
	#[inline(always)] // No-op.
	fn align(self) -> T {
		unsafe {
			// SAFETY: This trait is sealed and only implemented on and across compatible types.
			*(&self as *const Self).cast()
		}
	}

	/// Contextually thread-binds a reference, or not. Use only without qualification.
	#[allow(clippy::inline_always)]
	#[inline(always)] // No-op.
	fn align_ref(&self) -> &T {
		unsafe {
			// SAFETY: This trait is sealed and only implemented on and across compatible types.
			&*(self as *const Self).cast()
		}
	}
}

macro_rules! deanonymize_on_named {
	() => {
		/// When called on an opaque type, deanonymizes it into the underlying named type.
		///
		/// **Both** [`AutoSafe`] and [`Deanonymize`] must be in scope and the method must be called *without qualification* for this to work.
		///
		/// > Calling this method on a named type returns the value and type unchanged and produces a deprecation warning.
		#[deprecated = "Call of `.deanonymize()` on named type."]
		#[must_use]
		#[inline(always)] // No-op.
		pub fn deanonymize(self) -> Self {
			self
		}
	};
}

macro_rules! prefer_thread_safe_safe {
	{
		$(#[$($attrs:tt)*])* $(by value: $(#[$($value_attrs:tt)*])*)* $(by ref: $(#[$($ref_attrs:tt)*])*)?
	} => {
		/// Gently nudges the compiler to choose the [`ThreadSafe`] version of a value if both are possible.
		///
		/// This method is by value, so it will resolve with higher priority than the by-reference method on the [`ThreadBound`] type.
		///
		/// Note that not all tooling will show the correct overload here, but the compiler knows which to pick.
		$(#[$($attrs)*])*
		$($(#[$($value_attrs)*])*)?
		#[must_use]
		#[inline(always)] // No-op.
		pub fn prefer_thread_safe(self) -> Self {
			self
		}

		/// Gently nudges the compiler to choose the [`ThreadSafe`] version of a reference if both are possible.
		///
		/// This method is once by single reference, so it will resolve with higher priority than the twice-by-reference method on the [`ThreadBound`] type.
		///
		/// Note that not all tooling will show the correct overload here, but the compiler knows which to pick.
		$(#[$($attrs)*])*
		$($(#[$($ref_attrs)*])*)?
		#[must_use]
		#[inline(always)] // No-op.
		pub fn prefer_thread_safe_ref(&self) -> &Self {
			self
		}
	};
}

macro_rules! prefer_thread_safe_bound {
	() => {
		/// Gently nudges the compiler to choose the [`ThreadSafe`] version of a value if both are is possible.
		///
		/// This method is by reference, so it will resolve with lower priority than the by-value method on the [`ThreadSafe`] type.
		///
		/// Note that not all tooling will show the correct overload here, but the compiler knows which to pick.
		#[must_use]
		#[inline(always)] // Plain deref.
		pub fn prefer_thread_safe(&self) -> Self {
			*self
		}

		/// Gently nudges the compiler to choose the [`ThreadSafe`] version of a reference if both are is possible.
		///
		/// This method is twice by reference, so it will resolve with lower priority than the once-by-reference method on the [`ThreadSafe`] type.
		///
		/// Note that not all tooling will show the correct overload here, but the compiler knows which to pick.
		#[must_use]
		#[inline(always)] // Plain deref.
		pub fn prefer_thread_safe_ref<'b>(self: &'_ &'b Self) -> &'b Self {
			*self
		}
	};
}

impl<'a> Attribute<'a> {
	deanonymize_on_named!();
	prefer_thread_safe_safe! {
		///
		/// > Calling this method on [`Attribute`] produces a deprecation warning since the type is always [`ThreadSafe`].
		by value:
		#[deprecated = "Call of `.prefer_thread_safe()` on `Attribute`."]
		by ref:
		#[deprecated = "Call of `.prefer_thread_safe_ref()` on `Attribute`."]
	}
}

impl<'a> ElementCreationOptions<'a> {
	deanonymize_on_named!();
	prefer_thread_safe_safe! {
		///
		/// > Calling this method on [`ElementCreationOptions`] produces a deprecation warning since the type is always [`ThreadSafe`].
		by value:
		#[deprecated = "Call of `.prefer_thread_safe()` on `ElementCreationOptions`."]
		by ref:
		#[deprecated = "Call of `.prefer_thread_safe_ref()` on `ElementCreationOptions`."]
	}
}

impl EventBindingOptions {
	deanonymize_on_named!();
	prefer_thread_safe_safe! {
		///
		/// > Calling this method on [`EventBindingOptions`] produces a deprecation warning since the type is always [`ThreadSafe`].
		by value:
		#[deprecated = "Call of `.prefer_thread_safe()` on `EventBindingOptions`."]
		by ref:
		#[deprecated = "Call of `.prefer_thread_safe_ref()` on `EventBindingOptions`."]
	}
}

macro_rules! impl_auto_safety {
	($($Name:ident),*$(,)?) => {$(
		impl<'a, S: ThreadSafety> $Name<'a, S> {
			deanonymize_on_named!();
		}
		impl<'a> $Name<'a, ThreadSafe> {
			prefer_thread_safe_safe!();
		}
		impl<'a> $Name<'a, ThreadBound> {
			prefer_thread_safe_bound!();
		}
		impl<'a, V> Deanonymize<$Name<'a, ThreadSafe>> for V where
			V: Send + Sync + AutoSafe<$Name<'a, ThreadBound>>,
		{}

		/// Not derived from the [`Into`] constraints on `$Name` directly since those are too broad.
		impl<'a, S1, S2> Align<$Name<'a, S2>> for $Name<'a, S1>
		where
			S1: ThreadSafety + Into<S2>,
			S2: ThreadSafety,
		{}
	)*};
}

impl_auto_safety!(Element, EventBinding, Node, ReorderableFragment);

impl<S: ThreadSafety, C> CallbackRef<S, C>
where
	C: CallbackSignature,
{
	deanonymize_on_named!();
}
impl<C> CallbackRef<ThreadSafe, C>
where
	C: CallbackSignature,
{
	prefer_thread_safe_safe!();
}
impl<C> CallbackRef<ThreadBound, C>
where
	C: CallbackSignature,
{
	prefer_thread_safe_bound!();
}
impl<C, O> Deanonymize<CallbackRef<ThreadSafe, C>> for O
where
	C: CallbackSignature,
	O: Send + Sync + AutoSafe<CallbackRef<ThreadBound, C>>,
{
}
impl<S1, S2, C> Align<CallbackRef<S2, C>> for CallbackRef<S1, C>
where
	C: CallbackSignature,
	S1: ThreadSafety + Into<S2>,
	S2: ThreadSafety,
{
}

/// Mainly for use by frameworks. Canonically located at `auto_safe::AutoSafe_alias`.  
/// Creates a custom-visibility alias for [`auto_safety::AutoSafe`](`AutoSafe`).
///
/// See [`auto_safety`#limiting-autosafe-exposure](`crate::auto_safety`#limiting-autosafe-exposure) for more information.
#[macro_export]
macro_rules! AutoSafe_alias {
	($vis:vis $Name:ident) => {
		/// An alias for [`$crate::auto_safety::AutoSafe`] with custom visibility.
		$vis trait $Name<BoundVariant>: $crate::auto_safety::AutoSafe<BoundVariant>
		where
			BoundVariant: $crate::Vdom<ThreadSafety = $crate::ThreadBound>,
		{}
		impl<T, BoundVariant> $Name<BoundVariant> for T
		where
			T: $crate::auto_safety::AutoSafe<BoundVariant>,
			BoundVariant: $crate::Vdom<ThreadSafety = $crate::ThreadBound>,
		{}
	};
}

#[doc(inline)]
pub use crate::AutoSafe_alias;