fixed-record-macros 0.1.0

Procedural macro implementation for the fixed-record crate
Documentation
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
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
use crate::core::FieldMeta;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::DeriveInput;

/// Generates the optional `{StructName}List` helper.
/// optional な `{StructName}List` 補助型を生成します。
pub(super) fn gen_list_impl(input: &DeriveInput, metas: &[FieldMeta<'_>]) -> TokenStream {
    if !cfg!(feature = "list") {
        return quote!();
    }

    let struct_name = &input.ident;
    let struct_vis = &input.vis;
    let field_enum_name = format_ident!("{}Field", struct_name);
    let list_name = format_ident!("{}List", struct_name);
    let indices_name = format_ident!("{}ListIndices", struct_name);
    let rebuild_guard_name = format_ident!("{}ListRebuildGuard", struct_name);
    let edit_guard_name = format_ident!("{}ListEditGuard", struct_name);

    let index_fields = metas.iter().map(|meta| {
        let name = meta.name;
        let size = meta.size;
        quote! {
            #name: std::collections::BTreeMap<::fixed_record::Fixed<#size>, Vec<usize>>
        }
    });
    let index_record_fields = metas.iter().map(|meta| {
        let name = meta.name;
        quote! {
            Self::index_value(&mut indices.#name, id, record.#name);
        }
    });
    let unindex_record_fields = metas.iter().map(|meta| {
        let name = meta.name;
        quote! {
            Self::unindex_value(&mut indices.#name, id, record.#name);
        }
    });
    let increment_index_fields = metas.iter().map(|meta| {
        let name = meta.name;
        quote! {
            Self::increment_value_ids_at_or_after(&mut self.indices.#name, index);
        }
    });
    let decrement_index_fields = metas.iter().map(|meta| {
        let name = meta.name;
        quote! {
            Self::decrement_value_ids_after(&mut self.indices.#name, index);
        }
    });
    let indexed_prefix_arms = metas.iter().map(|meta| {
        let name = meta.name;
        let variant = &meta.variant;
        quote! {
            #field_enum_name::#variant => {
                Self::indexed_prefix_ids_for(&self.indices.#name, prefix, padding_only)
            }
        }
    });
    let first_indexed_prefix_arms = metas.iter().map(|meta| {
        let name = meta.name;
        let variant = &meta.variant;
        quote! {
            #field_enum_name::#variant => {
                Self::first_indexed_prefix_id_for(&self.indices.#name, prefix, padding_only)
            }
        }
    });
    let find_exact_by_arms = metas.iter().map(|meta| {
        let name = meta.name;
        let size = meta.size;
        let variant = &meta.variant;
        quote! {
            #field_enum_name::#variant => {
                let key = ::fixed_record::Fixed::<#size>::from_slice(raw_value)?;
                self.indices.#name.get(&key)
            }
        }
    });
    let first_exact_by_arms = metas.iter().map(|meta| {
        let name = meta.name;
        let size = meta.size;
        let variant = &meta.variant;
        quote! {
            #field_enum_name::#variant => {
                let key = ::fixed_record::Fixed::<#size>::from_slice(raw_value)?;
                self.indices.#name.get(&key).and_then(|ids| ids.first()).copied()
            }
        }
    });
    let find_range_by_arms = metas.iter().map(|meta| {
        let name = meta.name;
        let size = meta.size;
        let variant = &meta.variant;
        quote! {
            #field_enum_name::#variant => {
                Self::range_ids_for::<#size, R>(&self.indices.#name, range, field_name)?
            }
        }
    });
    let iter_sorted_by_arms = metas.iter().map(|meta| {
        let name = meta.name;
        let variant = &meta.variant;
        quote! {
            #field_enum_name::#variant => {
                for ids in self.indices.#name.values() {
                    records.extend(ids.iter().filter_map(|id| self.get(*id)));
                }
            }
        }
    });
    let first_sorted_by_arms = metas.iter().map(|meta| {
        let name = meta.name;
        let variant = &meta.variant;
        quote! {
            #field_enum_name::#variant => self
                .indices
                .#name
                .first_key_value()?
                .1
                .first()
                .copied()
        }
    });

    quote! {
        #[doc(hidden)]
        #[derive(Default)]
        struct #indices_name {
            #( #index_fields ),*
        }

        #[doc = "Stores boxed records in a vector and maintains field indexes for search and sorting."]
        #[doc = "Box 化したレコードを vector に保持し、検索・ソート用のフィールド索引を管理します。"]
        #[doc = "Each field index maps actual field bytes to the current vector indexes of matching records."]
        #[doc = "各フィールド索引は、実際のフィールドバイト列から、一致するレコードの現在の vector index へ対応付けます。"]
        #[doc = "Sorting moves boxes in the vector, not the record values allocated behind them."]
        #[doc = "ソート時は vector 内の Box が移動し、Box の先にあるレコード本体は移動しません。"]
        #[doc = "Record IDs are the current vector indexes, so IDs can change after removal or sorting."]
        #[doc = "レコード ID は現在の vector index です。そのため削除やソート後に ID は変わる可能性があります。"]
        #struct_vis struct #list_name {
            records: Vec<Box<#struct_name>>,
            indices: #indices_name,
        }

        #[doc(hidden)]
        struct #rebuild_guard_name<'a> {
            list: &'a mut #list_name,
        }

        impl Drop for #rebuild_guard_name<'_> {
            fn drop(&mut self) {
                self.list.rebuild_indices();
            }
        }

        #[doc(hidden)]
        struct #edit_guard_name<'a> {
            list: &'a mut #list_name,
            originals: Vec<(usize, #struct_name)>,
        }

        impl Drop for #edit_guard_name<'_> {
            fn drop(&mut self) {
                for (id, record) in &self.originals {
                    #list_name::unindex_record(&mut self.list.indices, *id, record);
                }

                let records = &self.list.records;
                let indices = &mut self.list.indices;
                for (id, _) in &self.originals {
                    if let Some(record) = records.get(*id) {
                        #list_name::index_record(indices, *id, record.as_ref());
                    }
                }
            }
        }

        impl #list_name {
            #[doc = "Creates an empty list."]
            #[doc = "空のリストを作成します。"]
            pub fn new() -> Self {
                Self {
                    records: Vec::new(),
                    indices: #indices_name::default(),
                }
            }

            #[doc = "Creates a list from records in their current order and builds all field indexes."]
            #[doc = "レコードの現在順からリストを作成し、全フィールド索引を構築します。"]
            pub fn from_records(records: Vec<#struct_name>) -> Self {
                let mut indices = #indices_name::default();
                let records = records
                    .into_iter()
                    .enumerate()
                    .map(|(id, record)| {
                        Self::index_record(&mut indices, id, &record);
                        Box::new(record)
                    })
                    .collect();
                Self { records, indices }
            }

            #[doc = "Reads all remaining records from a configured Reader and builds their field indexes."]
            #[doc = "設定済み Reader から残りの全レコードを読み込み、フィールド索引を構築します。"]
            pub fn read_from<R: std::io::BufRead>(
                reader: ::fixed_record::Reader<R, #struct_name>,
            ) -> Result<Self, ::fixed_record::error::Error> {
                reader.collect_list()
            }

            #[doc = "Returns the number of records."]
            #[doc = "レコード数を返します。"]
            pub fn len(&self) -> usize {
                self.records.len()
            }

            #[doc = "Returns whether there are no records."]
            #[doc = "レコードがないかを返します。"]
            pub fn is_empty(&self) -> bool {
                self.records.is_empty()
            }

            #[doc = "Removes all records and clears all field indexes."]
            #[doc = "全レコードを削除し、全フィールド索引を空にします。"]
            pub fn clear(&mut self) {
                self.records.clear();
                self.indices = #indices_name::default();
            }

            #[doc = "Retains only records for which the predicate returns `true` and rebuilds all field indexes."]
            #[doc = "predicate が `true` を返すレコードだけを残し、全フィールド索引を再構築します。"]
            #[doc = "Indexes are rebuilt by a drop guard even if the predicate unwinds."]
            #[doc = "predicate が unwind した場合も drop guard により索引を再構築します。"]
            #[deprecated(
                note = "performs a linear scan and rebuilds all field indexes; prefer indexed operations"
            )]
            pub fn retain(&mut self, mut keep: impl FnMut(&#struct_name) -> bool) {
                let mut guard = #rebuild_guard_name { list: self };
                guard
                    .list
                    .records
                    .retain(|record| keep(record.as_ref()));
            }

            #[doc = "Returns an iterator over records in the current order."]
            #[doc = "現在の順序でレコードを返すイテレータです。"]
            pub fn iter<'a>(&'a self) -> impl Iterator<Item = &'a #struct_name> + 'a {
                self.records.iter().map(|record| record.as_ref())
            }

            #[doc = "Returns the first record matching the predicate by scanning in the current order."]
            #[doc = "現在の順序で走査し、predicate に一致する最初のレコードを返します。"]
            #[deprecated(
                note = "performs a linear scan; use try_first_by*, first_sorted_by, or another indexed lookup"
            )]
            pub fn find(
                &self,
                mut predicate: impl FnMut(&#struct_name) -> bool,
            ) -> Option<&#struct_name> {
                self.records
                    .iter()
                    .map(|record| record.as_ref())
                    .find(|record| predicate(*record))
            }

            #[doc = "Returns all records matching the predicate by scanning in the current order."]
            #[doc = "現在の順序で走査し、predicate に一致する全レコードを返します。"]
            #[deprecated(
                note = "performs a linear scan; use try_find_by*, try_find_range_by, or another indexed lookup"
            )]
            pub fn find_all(
                &self,
                mut predicate: impl FnMut(&#struct_name) -> bool,
            ) -> Vec<&#struct_name> {
                self.records
                    .iter()
                    .map(|record| record.as_ref())
                    .filter(|record| predicate(*record))
                    .collect()
            }

            #[doc = "Mutates every record in the current order and rebuilds all field indexes afterward."]
            #[doc = "現在の順序で全レコードを変更し、処理後に全フィールド索引を再構築します。"]
            #[doc = "Mutable record references are confined to the callback and cannot be retained by the caller."]
            #[doc = "レコードの mutable 参照は callback 内に限定され、呼び出し側では保持できません。"]
            #[doc = "Indexes are rebuilt by a drop guard even if the callback unwinds."]
            #[doc = "callback が unwind した場合も drop guard により索引を再構築します。"]
            pub fn for_each_mut(&mut self, mut edit: impl FnMut(&mut #struct_name)) {
                let mut guard = #rebuild_guard_name { list: self };
                for record in &mut guard.list.records {
                    edit(record.as_mut());
                }
            }

            #[doc = "Adds one record to every field index."]
            #[doc = "1件のレコードを全フィールド索引へ追加します。"]
            fn index_record(indices: &mut #indices_name, id: usize, record: &#struct_name) {
                #( #index_record_fields )*
            }

            #[doc = "Adds one record ID to a typed field-value index."]
            #[doc = "型付きフィールド値索引へレコード ID を1件追加します。"]
            fn index_value<const N: usize>(
                field_index: &mut std::collections::BTreeMap<
                    ::fixed_record::Fixed<N>,
                    Vec<usize>,
                >,
                id: usize,
                value: ::fixed_record::Fixed<N>,
            ) {
                let ids = field_index.entry(value).or_default();
                if let Err(position) = ids.binary_search(&id) {
                    ids.insert(position, id);
                }
            }

            #[doc = "Removes one record from every field index."]
            #[doc = "1件のレコードを全フィールド索引から削除します。"]
            fn unindex_record(indices: &mut #indices_name, id: usize, record: &#struct_name) {
                #( #unindex_record_fields )*
            }

            #[doc = "Removes one record ID from a typed field-value index."]
            #[doc = "型付きフィールド値索引からレコード ID を1件削除します。"]
            fn unindex_value<const N: usize>(
                field_index: &mut std::collections::BTreeMap<
                    ::fixed_record::Fixed<N>,
                    Vec<usize>,
                >,
                id: usize,
                value: ::fixed_record::Fixed<N>,
            ) {
                let remove_value = if let Some(ids) = field_index.get_mut(&value) {
                    if let Ok(position) = ids.binary_search(&id) {
                        ids.remove(position);
                    }
                    ids.is_empty()
                } else {
                    false
                };

                if remove_value {
                    field_index.remove(&value);
                }
            }

            #[doc = "Rebuilds all field indexes from the current vector order."]
            #[doc = "現在の vector 順序から全フィールド索引を再構築します。"]
            fn rebuild_indices(&mut self) {
                let mut indices = #indices_name::default();
                for (id, record) in self.records.iter().enumerate() {
                    Self::index_record(&mut indices, id, record.as_ref());
                }
                self.indices = indices;
            }

            #[doc = "Increments indexed record IDs at or after an insertion position."]
            #[doc = "挿入位置以降の索引内レコード ID を1つ繰り上げます。"]
            fn increment_index_ids_at_or_after(&mut self, index: usize) {
                #( #increment_index_fields )*
            }

            #[doc = "Increments record IDs in one field index at or after an insertion position."]
            #[doc = "1つのフィールド索引で挿入位置以降のレコード ID を繰り上げます。"]
            fn increment_value_ids_at_or_after<const N: usize>(
                field_index: &mut std::collections::BTreeMap<
                    ::fixed_record::Fixed<N>,
                    Vec<usize>,
                >,
                index: usize,
            ) {
                for ids in field_index.values_mut() {
                    let first_shifted = ids.partition_point(|id| *id < index);
                    for id in &mut ids[first_shifted..] {
                        *id += 1;
                    }
                }
            }

            #[doc = "Decrements indexed record IDs after a removal position."]
            #[doc = "削除位置より後ろの索引内レコード ID を1つ繰り下げます。"]
            fn decrement_index_ids_after(&mut self, index: usize) {
                #( #decrement_index_fields )*
            }

            #[doc = "Decrements record IDs in one field index after a removal position."]
            #[doc = "1つのフィールド索引で削除位置より後ろのレコード ID を繰り下げます。"]
            fn decrement_value_ids_after<const N: usize>(
                field_index: &mut std::collections::BTreeMap<
                    ::fixed_record::Fixed<N>,
                    Vec<usize>,
                >,
                index: usize,
            ) {
                for ids in field_index.values_mut() {
                    let first_shifted = ids.partition_point(|id| *id <= index);
                    for id in &mut ids[first_shifted..] {
                        *id -= 1;
                    }
                }
            }

            #[doc = "Returns the exclusive upper bound for byte keys that share a prefix."]
            #[doc = "同じ prefix を持つバイトキーを範囲検索するための排他的上限を返します。"]
            fn prefix_upper_bound(prefix: &[u8]) -> Option<Vec<u8>> {
                let mut upper = prefix.to_vec();
                for index in (0..upper.len()).rev() {
                    if upper[index] != u8::MAX {
                        upper[index] += 1;
                        upper.truncate(index + 1);
                        return Some(upper);
                    }
                }
                None
            }

            #[doc = "Returns indexed record IDs whose field keys share a prefix."]
            #[doc = "フィールドキーが prefix を共有するレコード ID を索引から返します。"]
            fn indexed_prefix_ids(
                &self,
                field: #field_enum_name,
                prefix: &[u8],
                padding_only: bool,
            ) -> Vec<usize> {
                match field {
                    #( #indexed_prefix_arms ),*
                }
            }

            #[doc = "Returns record IDs in one typed field index whose keys share a prefix."]
            #[doc = "1つの型付きフィールド索引からキーが prefix を共有するレコード ID を返します。"]
            fn indexed_prefix_ids_for<const N: usize>(
                field_index: &std::collections::BTreeMap<
                    ::fixed_record::Fixed<N>,
                    Vec<usize>,
                >,
                prefix: &[u8],
                padding_only: bool,
            ) -> Vec<usize> {
                use std::ops::Bound::{Excluded, Included, Unbounded};

                let mut lower = ::fixed_record::Fixed::<N>::zeroed();
                lower.write_bytes(prefix);
                let upper = Self::prefix_upper_bound(prefix);
                let bounds = match upper {
                    Some(upper) => {
                        let mut value = ::fixed_record::Fixed::<N>::zeroed();
                        value.write_bytes(&upper);
                        (Included(lower), Excluded(value))
                    }
                    None => (Included(lower), Unbounded),
                };
                let mut ids = Vec::new();

                for (value, indexed_ids) in field_index.range(bounds) {
                    if !padding_only
                        || value.as_bytes()[prefix.len()..]
                            .iter()
                            .all(|byte| *byte == 0x00 || *byte == b' ')
                    {
                        ids.extend_from_slice(indexed_ids);
                    }
                }

                ids
            }

            #[doc = "Returns the first indexed record ID whose field key shares a prefix."]
            #[doc = "フィールドキーが prefix を共有する最初のレコード ID を索引から返します。"]
            fn first_indexed_prefix_id(
                &self,
                field: #field_enum_name,
                prefix: &[u8],
                padding_only: bool,
            ) -> Option<usize> {
                match field {
                    #( #first_indexed_prefix_arms ),*
                }
            }

            #[doc = "Returns the first record ID in one typed field index whose key shares a prefix."]
            #[doc = "1つの型付きフィールド索引からキーが prefix を共有する最初のレコード ID を返します。"]
            fn first_indexed_prefix_id_for<const N: usize>(
                field_index: &std::collections::BTreeMap<
                    ::fixed_record::Fixed<N>,
                    Vec<usize>,
                >,
                prefix: &[u8],
                padding_only: bool,
            ) -> Option<usize> {
                use std::ops::Bound::{Excluded, Included, Unbounded};

                let mut lower = ::fixed_record::Fixed::<N>::zeroed();
                lower.write_bytes(prefix);
                let upper = Self::prefix_upper_bound(prefix);
                let bounds = match upper {
                    Some(upper) => {
                        let mut value = ::fixed_record::Fixed::<N>::zeroed();
                        value.write_bytes(&upper);
                        (Included(lower), Excluded(value))
                    }
                    None => (Included(lower), Unbounded),
                };

                for (value, indexed_ids) in field_index.range(bounds) {
                    if (!padding_only
                        || value.as_bytes()[prefix.len()..]
                            .iter()
                            .all(|byte| *byte == 0x00 || *byte == b' '))
                        && let Some(id) = indexed_ids.first()
                    {
                        return Some(*id);
                    }
                }

                None
            }

            #[doc = "Maps current record IDs to borrowed records in current list order."]
            #[doc = "現在のレコード ID を、現在の List 順序に並ぶレコード参照へ変換します。"]
            fn records_for_ids<'a>(&'a self, mut ids: Vec<usize>) -> Vec<&#struct_name> {
                ids.sort_unstable();
                ids.into_iter().filter_map(|id| self.get(id)).collect()
            }

            #[doc = "Mutates records selected by private current indexes and repairs affected field indexes afterward."]
            #[doc = "非公開の現在 index で選択したレコードを変更し、処理後に影響するフィールド索引を修復します。"]
            fn edit_records_by_ids(
                &mut self,
                mut ids: Vec<usize>,
                mut edit: impl FnMut(&mut #struct_name),
            ) -> usize {
                ids.sort_unstable();
                ids.dedup();

                let originals: Vec<_> = ids
                    .into_iter()
                    .filter_map(|id| self.records.get(id).map(|record| (id, **record)))
                    .collect();
                let edited = originals.len();
                if originals.is_empty() {
                    return 0;
                }

                let mut guard = #edit_guard_name {
                    list: self,
                    originals,
                };
                for index in 0..guard.originals.len() {
                    let id = guard.originals[index].0;
                    edit(guard.list.records[id].as_mut());
                }
                edited
            }

            #[doc = "Mutates one record selected by a private current index and repairs its field indexes afterward."]
            #[doc = "非公開の現在 index で選択した1件を変更し、処理後にそのフィールド索引を修復します。"]
            fn edit_record_by_id(
                &mut self,
                id: Option<usize>,
                edit: impl FnOnce(&mut #struct_name),
            ) -> bool {
                let Some(id) = id else {
                    return false;
                };
                let Some(record) = self.records.get(id) else {
                    return false;
                };

                let mut guard = #edit_guard_name {
                    originals: vec![(id, **record)],
                    list: self,
                };
                edit(guard.list.records[id].as_mut());
                true
            }

            #[doc = "Validates a search value against the selected field width."]
            #[doc = "検索値が選択フィールドの幅に収まることを検証します。"]
            fn validate_search_width(
                field: #field_enum_name,
                value: &[u8],
            ) -> Result<(), ::fixed_record::error::Error> {
                let size = #struct_name::size_of(field);
                if value.len() > size {
                    return Err(::fixed_record::error::Error::FieldOverflow {
                        field: #struct_name::name_of(field),
                        size,
                        actual: value.len(),
                    });
                }
                Ok(())
            }

            #[doc = "Validates that a search value exactly matches the selected field width."]
            #[doc = "検索値が選択フィールドの幅と完全に一致することを検証します。"]
            fn validate_exact_search_width(
                field: #field_enum_name,
                value: &[u8],
            ) -> Result<(), ::fixed_record::error::Error> {
                let size = #struct_name::size_of(field);
                if value.len() < size {
                    return Err(::fixed_record::error::Error::TooShort);
                }
                Self::validate_search_width(field, value)
            }

            #[doc = "Returns private current indexes for an exact field match."]
            #[doc = "フィールドの完全一致に対応する非公開の現在 index を返します。"]
            fn try_find_ids_by(
                &self,
                field: #field_enum_name,
                value: &[u8],
            ) -> Result<Vec<usize>, ::fixed_record::error::Error> {
                Self::validate_exact_search_width(field, value)?;
                let raw_value = value;
                let ids = match field {
                    #( #find_exact_by_arms ),*
                };
                Ok(ids.cloned().unwrap_or_default())
            }

            #[doc = "Returns private current indexes matching a shortened padded field value."]
            #[doc = "短縮した padding 付きフィールド値に一致する非公開の現在 index を返します。"]
            fn try_find_ids_by_padded(
                &self,
                field: #field_enum_name,
                value: &[u8],
            ) -> Result<Vec<usize>, ::fixed_record::error::Error> {
                Self::validate_search_width(field, value)?;
                Ok(self.indexed_prefix_ids(field, value, true))
            }

            #[doc = "Returns private current indexes matching a field prefix."]
            #[doc = "フィールド prefix に一致する非公開の現在 index を返します。"]
            fn try_find_ids_by_prefix(
                &self,
                field: #field_enum_name,
                value: &[u8],
            ) -> Result<Vec<usize>, ::fixed_record::error::Error> {
                Self::validate_search_width(field, value)?;
                Ok(self.indexed_prefix_ids(field, value, false))
            }

            #[doc = "Returns private current indexes within a field range."]
            #[doc = "フィールド範囲内にある非公開の現在 index を返します。"]
            fn try_find_range_ids_by<R>(
                &self,
                field: #field_enum_name,
                range: &R,
            ) -> Result<Vec<usize>, ::fixed_record::error::Error>
            where
                R: ::fixed_record::traits::ByteRangeBounds,
            {
                let field_name = #struct_name::name_of(field);
                let ids = match field {
                    #( #find_range_by_arms ),*
                };
                Ok(ids)
            }

            #[doc = "Returns the private current index of the first exact field match."]
            #[doc = "フィールドの完全一致で最初の非公開の現在 index を返します。"]
            fn try_first_id_by(
                &self,
                field: #field_enum_name,
                value: &[u8],
            ) -> Result<Option<usize>, ::fixed_record::error::Error> {
                Self::validate_exact_search_width(field, value)?;
                let raw_value = value;
                Ok(match field {
                    #( #first_exact_by_arms ),*
                })
            }

            #[doc = "Returns the private current index of the first shortened padded-value match."]
            #[doc = "短縮した padding 付き値に一致する最初の非公開の現在 index を返します。"]
            fn try_first_id_by_padded(
                &self,
                field: #field_enum_name,
                value: &[u8],
            ) -> Result<Option<usize>, ::fixed_record::error::Error> {
                Self::validate_search_width(field, value)?;
                Ok(self.first_indexed_prefix_id(field, value, true))
            }

            #[doc = "Returns the private current index of the first field-prefix match."]
            #[doc = "フィールド prefix に一致する最初の非公開の現在 index を返します。"]
            fn try_first_id_by_prefix(
                &self,
                field: #field_enum_name,
                value: &[u8],
            ) -> Result<Option<usize>, ::fixed_record::error::Error> {
                Self::validate_search_width(field, value)?;
                Ok(self.first_indexed_prefix_id(field, value, false))
            }

            #[doc = "Appends a record and returns its current index as the ID."]
            #[doc = "レコードを末尾へ追加し、現在の index を ID として返します。"]
            pub fn push(&mut self, record: #struct_name) -> usize {
                let id = self.records.len();
                self.records.push(Box::new(record));
                Self::index_record(&mut self.indices, id, self.records[id].as_ref());
                id
            }

            #[doc = "Inserts a record at the specified index and updates shifted field indexes."]
            #[doc = "指定 index にレコードを挿入し、移動したフィールド索引を更新します。"]
            #[doc = "Returns `false` without changing the list when `index` is greater than `len()`."]
            #[doc = "`index` が `len()` より大きい場合は List を変更せず `false` を返します。"]
            pub fn insert(&mut self, index: usize, record: #struct_name) -> bool {
                if index > self.records.len() {
                    return false;
                }
                if index == self.records.len() {
                    self.push(record);
                    return true;
                }

                self.records.insert(index, Box::new(record));
                self.increment_index_ids_at_or_after(index);
                Self::index_record(&mut self.indices, index, self.records[index].as_ref());
                true
            }

            #[doc = "Returns the record at the specified current index."]
            #[doc = "指定された現在の index にあるレコードを返します。"]
            pub fn get(&self, id: usize) -> Option<&#struct_name> {
                self.records.get(id).map(|record| record.as_ref())
            }

            #[doc = "Replaces the record at the specified current index and updates all field indexes."]
            #[doc = "指定された現在の index にあるレコードを置き換え、全フィールド索引を更新します。"]
            pub fn update(&mut self, id: usize, record: #struct_name) -> bool {
                let Some(slot) = self.records.get_mut(id) else {
                    return false;
                };

                Self::unindex_record(&mut self.indices, id, slot.as_ref());
                **slot = record;
                Self::index_record(&mut self.indices, id, slot.as_ref());
                true
            }

            #[doc = "Removes the record at the specified current index and updates shifted field indexes."]
            #[doc = "指定された現在の index にあるレコードを削除し、移動したフィールド索引を更新します。"]
            pub fn remove(&mut self, id: usize) -> bool {
                if id >= self.records.len() {
                    return false;
                }

                let record = self.records.remove(id);
                Self::unindex_record(&mut self.indices, id, record.as_ref());
                self.decrement_index_ids_after(id);
                true
            }

            #[doc = "Removes and returns the last record without rebuilding the field indexes."]
            #[doc = "フィールド索引を再構築せず、末尾のレコードを削除して返します。"]
            pub fn pop(&mut self) -> Option<#struct_name> {
                let id = self.records.len().checked_sub(1)?;
                let record = self.records.pop()?;
                Self::unindex_record(&mut self.indices, id, record.as_ref());
                Some(*record)
            }

            #[doc = "Returns records whose specified field exactly matches the value using the field index."]
            #[doc = "フィールド索引を使い、指定フィールドが値と完全一致するレコードを返します。"]
            #[doc = "Returns `Error::TooShort` when the search value is shorter than the field."]
            #[doc = "検索値がフィールド幅より短い場合は `Error::TooShort` を返します。"]
            #[doc = "Returns `Error::FieldOverflow` when the search value is wider than the field."]
            #[doc = "検索値がフィールド幅を超える場合は `Error::FieldOverflow` を返します。"]
            pub fn try_find_by(
                &self,
                field: #field_enum_name,
                value: impl AsRef<[u8]>,
            ) -> Result<Vec<&#struct_name>, ::fixed_record::error::Error> {
                let ids = self.try_find_ids_by(field, value.as_ref())?;
                Ok(self.records_for_ids(ids))
            }

            #[doc = "Returns records whose specified field matches a possibly shortened padded value."]
            #[doc = "指定フィールドが、短縮可能な padding 付きの値と一致するレコードを返します。"]
            #[doc = "When the search value is shorter than the field width, trailing `0x00` or space bytes are accepted."]
            #[doc = "検索値がフィールド幅より短い場合は、後続バイトが `0x00` または半角スペースのレコードも一致します。"]
            #[doc = "Returns `Error::FieldOverflow` when the search value is wider than the field."]
            #[doc = "検索値がフィールド幅を超える場合は `Error::FieldOverflow` を返します。"]
            pub fn try_find_by_padded(
                &self,
                field: #field_enum_name,
                value: impl AsRef<[u8]>,
            ) -> Result<Vec<&#struct_name>, ::fixed_record::error::Error> {
                let ids = self.try_find_ids_by_padded(field, value.as_ref())?;
                Ok(self.records_for_ids(ids))
            }

            #[doc = "Returns records whose specified field starts with the value using the field index."]
            #[doc = "フィールド索引を使い、指定フィールドが値で始まるレコードを返します。"]
            #[doc = "When the search value is shorter than the field width, trailing bytes may contain any value."]
            #[doc = "検索値がフィールド幅より短い場合は、後続バイトの内容に関係なく一致します。"]
            #[doc = "Returns `Error::FieldOverflow` when the search value is wider than the field."]
            #[doc = "検索値がフィールド幅を超える場合は `Error::FieldOverflow` を返します。"]
            pub fn try_find_by_prefix(
                &self,
                field: #field_enum_name,
                value: impl AsRef<[u8]>,
            ) -> Result<Vec<&#struct_name>, ::fixed_record::error::Error> {
                let ids = self.try_find_ids_by_prefix(field, value.as_ref())?;
                Ok(self.records_for_ids(ids))
            }

            #[doc = "Returns records whose specified field value is within the indexed range."]
            #[doc = "指定フィールドの値が索引上の範囲内にあるレコードを返します。"]
            #[doc = "Short bounds allow any trailing field bytes."]
            #[doc = "短い境界値では、フィールドの後続バイトを任意として扱います。"]
            #[doc = "Returns `Error::FieldOverflow` when either bound is wider than the field."]
            #[doc = "いずれかの境界値がフィールド幅を超える場合は `Error::FieldOverflow` を返します。"]
            #[doc = "Returns `Error::InvalidRange` when the start value is greater than the end value."]
            #[doc = "開始値が終了値より大きい場合は `Error::InvalidRange` を返します。"]
            pub fn try_find_range_by<R>(
                &self,
                field: #field_enum_name,
                range: R,
            ) -> Result<Vec<&#struct_name>, ::fixed_record::error::Error>
            where
                R: ::fixed_record::traits::ByteRangeBounds,
            {
                let ids = self.try_find_range_ids_by(field, &range)?;
                Ok(self.records_for_ids(ids))
            }

            #[doc = "Returns record IDs within a range from one typed field index."]
            #[doc = "1つの型付きフィールド索引から範囲内のレコード ID を返します。"]
            fn range_ids_for<const N: usize, R>(
                field_index: &std::collections::BTreeMap<
                    ::fixed_record::Fixed<N>,
                    Vec<usize>,
                >,
                range: &R,
                field_name: &'static str,
            ) -> Result<Vec<usize>, ::fixed_record::error::Error>
            where
                R: ::fixed_record::traits::ByteRangeBounds,
            {
                use std::ops::Bound::{Excluded, Included, Unbounded};

                let start_bound = range.start_bound_bytes();
                let end_bound = range.end_bound_bytes();
                let start_bytes = match start_bound {
                    Included(value) | Excluded(value) => Some(value),
                    Unbounded => None,
                };
                let end_bytes = match end_bound {
                    Included(value) | Excluded(value) => Some(value),
                    Unbounded => None,
                };

                for value in [start_bytes, end_bytes].into_iter().flatten() {
                    if value.len() > N {
                        return Err(::fixed_record::error::Error::FieldOverflow {
                            field: field_name,
                            size: N,
                            actual: value.len(),
                        });
                    }
                }

                let start = match start_bound {
                    Included(value) => {
                        let mut bound = ::fixed_record::Fixed::<N>::zeroed();
                        bound.write_bytes(value);
                        Included(bound)
                    }
                    Excluded(value) => {
                        let mut bound = ::fixed_record::Fixed::<N>::zeroed();
                        bound.write_bytes(value);
                        Excluded(bound)
                    }
                    Unbounded => Unbounded,
                };
                let end = match end_bound {
                    Included(value) => {
                        let mut bound = ::fixed_record::Fixed::<N>::filled(u8::MAX);
                        bound.write_bytes(value);
                        Included(bound)
                    }
                    Excluded(value) => {
                        let mut bound = ::fixed_record::Fixed::<N>::filled(u8::MAX);
                        bound.write_bytes(value);
                        Excluded(bound)
                    }
                    Unbounded => Unbounded,
                };
                let start_is_excluded = matches!(&start, Excluded(_));
                let end_is_excluded = matches!(&end, Excluded(_));
                let bounds_order = match (&start, &end) {
                    (
                        Included(start_value) | Excluded(start_value),
                        Included(end_value) | Excluded(end_value),
                    ) => Some(start_value.cmp(end_value)),
                    _ => None,
                };
                if bounds_order == Some(std::cmp::Ordering::Greater) {
                    return Err(::fixed_record::error::Error::InvalidRange {
                        field: field_name,
                        start: start_bytes.unwrap_or_default().to_vec(),
                        end: end_bytes.unwrap_or_default().to_vec(),
                    });
                }
                if bounds_order == Some(std::cmp::Ordering::Equal)
                    && start_is_excluded
                    && end_is_excluded
                {
                    return Ok(Vec::new());
                }

                let mut ids = Vec::new();
                for indexed_ids in field_index.range((start, end)).map(|(_, ids)| ids) {
                    ids.extend_from_slice(indexed_ids);
                }
                Ok(ids)
            }

            #[doc = "Returns an iterator over records in indexed ascending order by the specified field."]
            #[doc = "指定フィールドの索引上の昇順でレコードを返すイテレータです。"]
            pub fn iter_sorted_by<'a, const N: usize>(
                &'a self,
                field: #field_enum_name,
            ) -> impl Iterator<Item = &'a #struct_name> + 'a {
                let mut records = Vec::with_capacity(self.records.len());
                match field {
                    #( #iter_sorted_by_arms ),*
                }
                records.into_iter()
            }

            #[doc = "Sorts records by comparing all fields in declaration order and rebuilds the indexes."]
            #[doc = "レコードを定義順の全フィールド比較でソートし、索引を再構築します。"]
            pub fn sort(&mut self) {
                self.records.sort_by(|left, right| {
                    left.as_ref().compare_all_fields(right.as_ref())
                });
                self.rebuild_indices();
            }

            #[doc = "Sorts records by the specified field priority order and rebuilds the indexes."]
            #[doc = "指定したフィールド優先順でレコードをソートし、索引を再構築します。"]
            pub fn sort_by(&mut self, fields: &[#field_enum_name]) {
                self.records.sort_by(|left, right| {
                    left.as_ref().compare_by_fields(right.as_ref(), fields)
                });
                self.rebuild_indices();
            }

            #[doc = "Returns the first record in the current order."]
            #[doc = "現在の順序で最初のレコードを返します。"]
            pub fn first(&self) -> Option<&#struct_name> {
                self.records.first().map(|record| record.as_ref())
            }

            #[doc = "Returns the first record whose specified field exactly matches the value."]
            #[doc = "指定フィールドが値と完全一致する最初のレコードを返します。"]
            #[doc = "Returns `Error::TooShort` when the search value is shorter than the field."]
            #[doc = "検索値がフィールド幅より短い場合は `Error::TooShort` を返します。"]
            #[doc = "Returns `Error::FieldOverflow` when the search value is wider than the field."]
            #[doc = "検索値がフィールド幅を超える場合は `Error::FieldOverflow` を返します。"]
            pub fn try_first_by(
                &self,
                field: #field_enum_name,
                value: impl AsRef<[u8]>,
            ) -> Result<Option<&#struct_name>, ::fixed_record::error::Error> {
                let id = self.try_first_id_by(field, value.as_ref())?;
                Ok(id.and_then(|id| self.get(id)))
            }

            #[doc = "Returns the first record in indexed ascending order by the specified field."]
            #[doc = "指定フィールドの索引上の昇順で最初のレコードを返します。"]
            pub fn first_sorted_by(&self, field: #field_enum_name) -> Option<&#struct_name> {
                let id = match field {
                    #( #first_sorted_by_arms ),*
                }?;
                self.get(id)
            }

            #[doc = "Returns the first indexed record matching a possibly shortened padded value."]
            #[doc = "短縮可能な padding 付きの値と一致する、索引上で最初のレコードを返します。"]
            #[doc = "When the search value is shorter than the field width, trailing `0x00` or space bytes are accepted."]
            #[doc = "検索値がフィールド幅より短い場合は、後続バイトが `0x00` または半角スペースのレコードも一致します。"]
            #[doc = "Returns `Error::FieldOverflow` when the search value is wider than the field."]
            #[doc = "検索値がフィールド幅を超える場合は `Error::FieldOverflow` を返します。"]
            pub fn try_first_by_padded(
                &self,
                field: #field_enum_name,
                value: impl AsRef<[u8]>,
            ) -> Result<Option<&#struct_name>, ::fixed_record::error::Error> {
                let id = self.try_first_id_by_padded(field, value.as_ref())?;
                Ok(id.and_then(|id| self.get(id)))
            }

            #[doc = "Returns the first indexed prefix match for the specified field."]
            #[doc = "指定フィールドが値で始まるレコードのうち、索引上で最初のものを返します。"]
            #[doc = "Returns `Error::FieldOverflow` when the search value is wider than the field."]
            #[doc = "検索値がフィールド幅を超える場合は `Error::FieldOverflow` を返します。"]
            pub fn try_first_by_prefix(
                &self,
                field: #field_enum_name,
                value: impl AsRef<[u8]>,
            ) -> Result<Option<&#struct_name>, ::fixed_record::error::Error> {
                let id = self.try_first_id_by_prefix(field, value.as_ref())?;
                Ok(id.and_then(|id| self.get(id)))
            }

            #[doc = "Mutates every record whose specified field exactly matches the value."]
            #[doc = "指定フィールドが値と完全一致する全レコードを変更します。"]
            #[doc = "Returns the number of edited records without exposing their internal current indexes."]
            #[doc = "内部の現在 index を公開せず、変更したレコード数を返します。"]
            pub fn try_edit_by(
                &mut self,
                field: #field_enum_name,
                value: impl AsRef<[u8]>,
                edit: impl FnMut(&mut #struct_name),
            ) -> Result<usize, ::fixed_record::error::Error> {
                let ids = self.try_find_ids_by(field, value.as_ref())?;
                Ok(self.edit_records_by_ids(ids, edit))
            }

            #[doc = "Mutates every record matching a possibly shortened padded field value."]
            #[doc = "短縮可能な padding 付きフィールド値に一致する全レコードを変更します。"]
            #[doc = "Returns the number of edited records without exposing their internal current indexes."]
            #[doc = "内部の現在 index を公開せず、変更したレコード数を返します。"]
            pub fn try_edit_by_padded(
                &mut self,
                field: #field_enum_name,
                value: impl AsRef<[u8]>,
                edit: impl FnMut(&mut #struct_name),
            ) -> Result<usize, ::fixed_record::error::Error> {
                let ids = self.try_find_ids_by_padded(field, value.as_ref())?;
                Ok(self.edit_records_by_ids(ids, edit))
            }

            #[doc = "Mutates every record whose specified field starts with the value."]
            #[doc = "指定フィールドが値で始まる全レコードを変更します。"]
            #[doc = "Returns the number of edited records without exposing their internal current indexes."]
            #[doc = "内部の現在 index を公開せず、変更したレコード数を返します。"]
            pub fn try_edit_by_prefix(
                &mut self,
                field: #field_enum_name,
                value: impl AsRef<[u8]>,
                edit: impl FnMut(&mut #struct_name),
            ) -> Result<usize, ::fixed_record::error::Error> {
                let ids = self.try_find_ids_by_prefix(field, value.as_ref())?;
                Ok(self.edit_records_by_ids(ids, edit))
            }

            #[doc = "Mutates every record whose specified field value is within the indexed range."]
            #[doc = "指定フィールドの値が索引上の範囲内にある全レコードを変更します。"]
            #[doc = "Returns the number of edited records without exposing their internal current indexes."]
            #[doc = "内部の現在 index を公開せず、変更したレコード数を返します。"]
            pub fn try_edit_range_by<R>(
                &mut self,
                field: #field_enum_name,
                range: R,
                edit: impl FnMut(&mut #struct_name),
            ) -> Result<usize, ::fixed_record::error::Error>
            where
                R: ::fixed_record::traits::ByteRangeBounds,
            {
                let ids = self.try_find_range_ids_by(field, &range)?;
                Ok(self.edit_records_by_ids(ids, edit))
            }

            #[doc = "Mutates the first record whose specified field exactly matches the value."]
            #[doc = "指定フィールドが値と完全一致する最初のレコードを変更します。"]
            #[doc = "Returns whether a record was edited without exposing its internal current index."]
            #[doc = "内部の現在 index を公開せず、レコードを変更したかを返します。"]
            pub fn try_edit_first_by(
                &mut self,
                field: #field_enum_name,
                value: impl AsRef<[u8]>,
                edit: impl FnOnce(&mut #struct_name),
            ) -> Result<bool, ::fixed_record::error::Error> {
                let id = self.try_first_id_by(field, value.as_ref())?;
                Ok(self.edit_record_by_id(id, edit))
            }

            #[doc = "Mutates the first record matching a possibly shortened padded field value."]
            #[doc = "短縮可能な padding 付きフィールド値に一致する最初のレコードを変更します。"]
            #[doc = "Returns whether a record was edited without exposing its internal current index."]
            #[doc = "内部の現在 index を公開せず、レコードを変更したかを返します。"]
            pub fn try_edit_first_by_padded(
                &mut self,
                field: #field_enum_name,
                value: impl AsRef<[u8]>,
                edit: impl FnOnce(&mut #struct_name),
            ) -> Result<bool, ::fixed_record::error::Error> {
                let id = self.try_first_id_by_padded(field, value.as_ref())?;
                Ok(self.edit_record_by_id(id, edit))
            }

            #[doc = "Mutates the first record whose specified field starts with the value."]
            #[doc = "指定フィールドが値で始まる最初のレコードを変更します。"]
            #[doc = "Returns whether a record was edited without exposing its internal current index."]
            #[doc = "内部の現在 index を公開せず、レコードを変更したかを返します。"]
            pub fn try_edit_first_by_prefix(
                &mut self,
                field: #field_enum_name,
                value: impl AsRef<[u8]>,
                edit: impl FnOnce(&mut #struct_name),
            ) -> Result<bool, ::fixed_record::error::Error> {
                let id = self.try_first_id_by_prefix(field, value.as_ref())?;
                Ok(self.edit_record_by_id(id, edit))
            }

            #[doc = "Returns all current record IDs."]
            #[doc = "現在の全レコード ID を返します。"]
            pub fn all_ids(&self) -> Vec<usize> {
                (0..self.records.len()).collect()
            }
        }

        impl Default for #list_name {
            #[doc = "Creates an empty list."]
            #[doc = "空のリストを作成します。"]
            fn default() -> Self {
                Self::new()
            }
        }

        impl ::fixed_record::traits::RecordWithList for #struct_name {
            type List = #list_name;
        }

        impl From<Vec<#struct_name>> for #list_name {
            #[doc = "Creates a list from a vector of records."]
            #[doc = "レコードの vector からリストを作成します。"]
            fn from(records: Vec<#struct_name>) -> Self {
                Self::from_records(records)
            }
        }
    }
}