-
Notifications
You must be signed in to change notification settings - Fork 891
/
name.rs
805 lines (692 loc) · 22.9 KB
/
name.rs
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
use std::fmt::{Debug, Display, Formatter, Write};
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use crate::{nodes, Expr};
/// A representation of a qualified name, like `typing.List`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct QualifiedName<'a>(SegmentsVec<'a>);
impl<'a> QualifiedName<'a> {
/// Create a [`QualifiedName`] from a dotted name.
///
/// ```rust
/// # use ruff_python_ast::name::QualifiedName;
///
/// assert_eq!(QualifiedName::from_dotted_name("typing.List").segments(), ["typing", "List"]);
/// assert_eq!(QualifiedName::from_dotted_name("list").segments(), ["", "list"]);
/// ```
#[inline]
pub fn from_dotted_name(name: &'a str) -> Self {
if let Some(dot) = name.find('.') {
let mut builder = QualifiedNameBuilder::default();
builder.push(&name[..dot]);
builder.extend(name[dot + 1..].split('.'));
builder.build()
} else {
Self::builtin(name)
}
}
/// Creates a name that's guaranteed not be a built in
#[inline]
pub fn user_defined(name: &'a str) -> Self {
name.split('.').collect()
}
/// Creates a qualified name for a built in
#[inline]
pub fn builtin(name: &'a str) -> Self {
debug_assert!(!name.contains('.'));
Self(SegmentsVec::Stack(SegmentsStack::from_slice(&["", name])))
}
#[inline]
pub fn segments(&self) -> &[&'a str] {
self.0.as_slice()
}
/// If the first segment is empty, the `CallPath` is that of a builtin.
/// Ex) `["", "bool"]` -> `"bool"`
pub fn is_builtin(&self) -> bool {
matches!(self.segments(), ["", ..])
}
pub fn is_user_defined(&self) -> bool {
!self.is_builtin()
}
/// If the call path is dot-prefixed, it's an unresolved relative import.
/// Ex) `[".foo", "bar"]` -> `".foo.bar"`
pub fn is_unresolved_import(&self) -> bool {
matches!(self.segments(), [".", ..])
}
pub fn starts_with(&self, other: &QualifiedName<'_>) -> bool {
self.segments().starts_with(other.segments())
}
/// Appends a member to the qualified name.
#[must_use]
pub fn append_member(self, member: &'a str) -> Self {
let mut inner = self.0;
inner.push(member);
Self(inner)
}
}
impl Display for QualifiedName<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let segments = self.segments();
if self.is_unresolved_import() {
let mut iter = segments.iter();
for segment in iter.by_ref() {
if *segment == "." {
f.write_char('.')?;
} else {
f.write_str(segment)?;
break;
}
}
for segment in iter {
f.write_char('.')?;
f.write_str(segment)?;
}
} else {
let segments = if self.is_builtin() {
&segments[1..]
} else {
segments
};
let mut first = true;
for segment in segments {
if !first {
f.write_char('.')?;
}
f.write_str(segment)?;
first = false;
}
}
Ok(())
}
}
impl<'a> FromIterator<&'a str> for QualifiedName<'a> {
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
Self(SegmentsVec::from_iter(iter))
}
}
#[derive(Debug, Clone, Default)]
pub struct QualifiedNameBuilder<'a> {
segments: SegmentsVec<'a>,
}
impl<'a> QualifiedNameBuilder<'a> {
pub fn with_capacity(capacity: usize) -> Self {
Self {
segments: SegmentsVec::with_capacity(capacity),
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.segments.is_empty()
}
#[inline]
pub fn push(&mut self, segment: &'a str) {
self.segments.push(segment);
}
#[inline]
pub fn pop(&mut self) {
self.segments.pop();
}
#[inline]
pub fn extend(&mut self, segments: impl IntoIterator<Item = &'a str>) {
self.segments.extend(segments);
}
#[inline]
pub fn extend_from_slice(&mut self, segments: &[&'a str]) {
self.segments.extend_from_slice(segments);
}
pub fn build(self) -> QualifiedName<'a> {
QualifiedName(self.segments)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UnqualifiedName<'a>(SegmentsVec<'a>);
impl<'a> UnqualifiedName<'a> {
/// Convert an `Expr` to its [`UnqualifiedName`] (like `["typing", "List"]`).
pub fn from_expr(expr: &'a Expr) -> Option<Self> {
// Unroll the loop up to eight times, to match the maximum number of expected attributes.
// In practice, unrolling appears to give about a 4x speed-up on this hot path.
let attr1 = match expr {
Expr::Attribute(attr1) => attr1,
// Ex) `foo`
Expr::Name(nodes::ExprName { id, .. }) => {
return Some(Self::from_slice(&[id.as_str()]))
}
_ => return None,
};
let attr2 = match attr1.value.as_ref() {
Expr::Attribute(attr2) => attr2,
// Ex) `foo.bar`
Expr::Name(nodes::ExprName { id, .. }) => {
return Some(Self::from_slice(&[id.as_str(), attr1.attr.as_str()]))
}
_ => return None,
};
let attr3 = match attr2.value.as_ref() {
Expr::Attribute(attr3) => attr3,
// Ex) `foo.bar.baz`
Expr::Name(nodes::ExprName { id, .. }) => {
return Some(Self::from_slice(&[
id.as_str(),
attr2.attr.as_str(),
attr1.attr.as_str(),
]));
}
_ => return None,
};
let attr4 = match attr3.value.as_ref() {
Expr::Attribute(attr4) => attr4,
// Ex) `foo.bar.baz.bop`
Expr::Name(nodes::ExprName { id, .. }) => {
return Some(Self::from_slice(&[
id.as_str(),
attr3.attr.as_str(),
attr2.attr.as_str(),
attr1.attr.as_str(),
]));
}
_ => return None,
};
let attr5 = match attr4.value.as_ref() {
Expr::Attribute(attr5) => attr5,
// Ex) `foo.bar.baz.bop.bap`
Expr::Name(nodes::ExprName { id, .. }) => {
return Some(Self::from_slice(&[
id.as_str(),
attr4.attr.as_str(),
attr3.attr.as_str(),
attr2.attr.as_str(),
attr1.attr.as_str(),
]));
}
_ => return None,
};
let attr6 = match attr5.value.as_ref() {
Expr::Attribute(attr6) => attr6,
// Ex) `foo.bar.baz.bop.bap.bab`
Expr::Name(nodes::ExprName { id, .. }) => {
return Some(Self::from_slice(&[
id.as_str(),
attr5.attr.as_str(),
attr4.attr.as_str(),
attr3.attr.as_str(),
attr2.attr.as_str(),
attr1.attr.as_str(),
]));
}
_ => return None,
};
let attr7 = match attr6.value.as_ref() {
Expr::Attribute(attr7) => attr7,
// Ex) `foo.bar.baz.bop.bap.bab.bob`
Expr::Name(nodes::ExprName { id, .. }) => {
return Some(Self::from_slice(&[
id.as_str(),
attr6.attr.as_str(),
attr5.attr.as_str(),
attr4.attr.as_str(),
attr3.attr.as_str(),
attr2.attr.as_str(),
attr1.attr.as_str(),
]));
}
_ => return None,
};
let attr8 = match attr7.value.as_ref() {
Expr::Attribute(attr8) => attr8,
// Ex) `foo.bar.baz.bop.bap.bab.bob.bib`
Expr::Name(nodes::ExprName { id, .. }) => {
return Some(Self(SegmentsVec::from([
id.as_str(),
attr7.attr.as_str(),
attr6.attr.as_str(),
attr5.attr.as_str(),
attr4.attr.as_str(),
attr3.attr.as_str(),
attr2.attr.as_str(),
attr1.attr.as_str(),
])));
}
_ => return None,
};
let mut segments = Vec::with_capacity(SMALL_LEN * 2);
let mut current = &*attr8.value;
loop {
current = match current {
Expr::Attribute(attr) => {
segments.push(attr.attr.as_str());
&*attr.value
}
Expr::Name(nodes::ExprName { id, .. }) => {
segments.push(id.as_str());
break;
}
_ => {
return None;
}
}
}
segments.reverse();
// Append the attributes we visited before calling into the recursion.
segments.extend_from_slice(&[
attr8.attr.as_str(),
attr7.attr.as_str(),
attr6.attr.as_str(),
attr5.attr.as_str(),
attr4.attr.as_str(),
attr3.attr.as_str(),
attr2.attr.as_str(),
attr1.attr.as_str(),
]);
Some(Self(SegmentsVec::from(segments)))
}
#[inline]
pub fn from_slice(segments: &[&'a str]) -> Self {
Self(SegmentsVec::from_slice(segments))
}
pub fn segments(&self) -> &[&'a str] {
self.0.as_slice()
}
}
impl Display for UnqualifiedName<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut first = true;
for segment in self.segments() {
if !first {
f.write_char('.')?;
}
f.write_str(segment)?;
first = false;
}
Ok(())
}
}
impl<'a> FromIterator<&'a str> for UnqualifiedName<'a> {
#[inline]
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}
/// A smallvec like storage for qualified and unqualified name segments.
///
/// Stores up to 8 segments inline, and falls back to a heap-allocated vector for names with more segments.
///
/// ## Note
/// The implementation doesn't use `SmallVec` v1 because its type definition has a variance problem.
/// The incorrect variance leads the lifetime inference in the `SemanticModel` astray, causing
/// all sort of "strange" lifetime errors. We can switch back to `SmallVec` when v2 is released.
#[derive(Clone)]
enum SegmentsVec<'a> {
Stack(SegmentsStack<'a>),
Heap(Vec<&'a str>),
}
impl<'a> SegmentsVec<'a> {
/// Creates an empty segment vec.
fn new() -> Self {
Self::Stack(SegmentsStack::default())
}
/// Creates a segment vec that has reserved storage for up to `capacity` items.
fn with_capacity(capacity: usize) -> Self {
if capacity <= SMALL_LEN {
Self::new()
} else {
Self::Heap(Vec::with_capacity(capacity))
}
}
#[cfg(test)]
const fn is_spilled(&self) -> bool {
matches!(self, SegmentsVec::Heap(_))
}
/// Initializes the segments from a slice.
#[inline]
fn from_slice(slice: &[&'a str]) -> Self {
if slice.len() <= SMALL_LEN {
SegmentsVec::Stack(SegmentsStack::from_slice(slice))
} else {
SegmentsVec::Heap(slice.to_vec())
}
}
/// Returns the segments as a slice.
#[inline]
fn as_slice(&self) -> &[&'a str] {
match self {
Self::Stack(stack) => stack.as_slice(),
Self::Heap(heap) => heap.as_slice(),
}
}
/// Pushes `name` to the end of the segments.
///
/// Spills to the heap if the segments are stored on the stack and the 9th segment is pushed.
#[inline]
fn push(&mut self, name: &'a str) {
match self {
SegmentsVec::Stack(stack) => {
if let Err(segments) = stack.push(name) {
*self = SegmentsVec::Heap(segments);
}
}
SegmentsVec::Heap(heap) => {
heap.push(name);
}
}
}
/// Pops the last segment from the end and returns it.
///
/// Returns `None` if the vector is empty.
#[inline]
fn pop(&mut self) -> Option<&'a str> {
match self {
SegmentsVec::Stack(stack) => stack.pop(),
SegmentsVec::Heap(heap) => heap.pop(),
}
}
#[inline]
fn extend_from_slice(&mut self, slice: &[&'a str]) {
match self {
SegmentsVec::Stack(stack) => {
if let Err(segments) = stack.extend_from_slice(slice) {
*self = SegmentsVec::Heap(segments);
}
}
SegmentsVec::Heap(heap) => heap.extend_from_slice(slice),
}
}
}
impl Default for SegmentsVec<'_> {
fn default() -> Self {
Self::new()
}
}
impl Debug for SegmentsVec<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_list().entries(self.as_slice()).finish()
}
}
impl<'a> Deref for SegmentsVec<'a> {
type Target = [&'a str];
fn deref(&self) -> &Self::Target {
self.as_slice()
}
}
impl<'a, 'b> PartialEq<SegmentsVec<'b>> for SegmentsVec<'a> {
fn eq(&self, other: &SegmentsVec<'b>) -> bool {
self.as_slice() == other.as_slice()
}
}
impl Eq for SegmentsVec<'_> {}
impl Hash for SegmentsVec<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_slice().hash(state);
}
}
impl<'a> FromIterator<&'a str> for SegmentsVec<'a> {
#[inline]
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
let mut segments = SegmentsVec::default();
segments.extend(iter);
segments
}
}
impl<'a> From<[&'a str; 8]> for SegmentsVec<'a> {
#[inline]
fn from(segments: [&'a str; 8]) -> Self {
SegmentsVec::Stack(SegmentsStack {
segments,
len: segments.len(),
})
}
}
impl<'a> From<Vec<&'a str>> for SegmentsVec<'a> {
#[inline]
fn from(segments: Vec<&'a str>) -> Self {
SegmentsVec::Heap(segments)
}
}
impl<'a> Extend<&'a str> for SegmentsVec<'a> {
#[inline]
fn extend<T: IntoIterator<Item = &'a str>>(&mut self, iter: T) {
match self {
SegmentsVec::Stack(stack) => {
if let Err(segments) = stack.extend(iter) {
*self = SegmentsVec::Heap(segments);
}
}
SegmentsVec::Heap(heap) => {
heap.extend(iter);
}
}
}
}
const SMALL_LEN: usize = 8;
#[derive(Debug, Clone, Default)]
struct SegmentsStack<'a> {
segments: [&'a str; SMALL_LEN],
len: usize,
}
impl<'a> SegmentsStack<'a> {
#[inline]
fn from_slice(slice: &[&'a str]) -> Self {
assert!(slice.len() <= SMALL_LEN);
let mut segments: [&'a str; SMALL_LEN] = Default::default();
segments[..slice.len()].copy_from_slice(slice);
SegmentsStack {
segments,
len: slice.len(),
}
}
const fn capacity(&self) -> usize {
SMALL_LEN - self.len
}
#[inline]
fn as_slice(&self) -> &[&'a str] {
&self.segments[..self.len]
}
/// Pushes `name` to the end of the segments.
///
/// Returns `Err` with a `Vec` containing all items (including `name`) if there's not enough capacity to push the name.
#[inline]
fn push(&mut self, name: &'a str) -> Result<(), Vec<&'a str>> {
if self.len < self.segments.len() {
self.segments[self.len] = name;
self.len += 1;
Ok(())
} else {
let mut segments = Vec::with_capacity(self.len * 2);
segments.extend_from_slice(&self.segments);
segments.push(name);
Err(segments)
}
}
/// Reserves spaces for `additional` segments.
///
/// Returns `Err` with a `Vec` containing all segments and a capacity to store `additional` segments if
/// the stack needs to spill over to the heap to store `additional` segments.
#[inline]
fn reserve(&mut self, additional: usize) -> Result<(), Vec<&'a str>> {
if self.capacity() >= additional {
Ok(())
} else {
let mut segments = Vec::with_capacity(self.len + additional);
segments.extend_from_slice(self.as_slice());
Err(segments)
}
}
#[inline]
fn pop(&mut self) -> Option<&'a str> {
if self.len > 0 {
self.len -= 1;
Some(self.segments[self.len])
} else {
None
}
}
/// Extends the segments by appending `slice` to the end.
///
/// Returns `Err` with a `Vec` containing all segments and the segments in `slice` if there's not enough capacity to append the names.
#[inline]
fn extend_from_slice(&mut self, slice: &[&'a str]) -> Result<(), Vec<&'a str>> {
let new_len = self.len + slice.len();
if slice.len() <= self.capacity() {
self.segments[self.len..new_len].copy_from_slice(slice);
self.len = new_len;
Ok(())
} else {
let mut segments = Vec::with_capacity(new_len);
segments.extend_from_slice(self.as_slice());
segments.extend_from_slice(slice);
Err(segments)
}
}
#[inline]
fn extend<I>(&mut self, iter: I) -> Result<(), Vec<&'a str>>
where
I: IntoIterator<Item = &'a str>,
{
let mut iter = iter.into_iter();
let (lower, _) = iter.size_hint();
// There's not enough space to store the lower bound of items. Spill to the heap.
if let Err(mut segments) = self.reserve(lower) {
segments.extend(iter);
return Err(segments);
}
// Copy over up to capacity items
for name in iter.by_ref().take(self.capacity()) {
self.segments[self.len] = name;
self.len += 1;
}
let Some(item) = iter.next() else {
// There are no more items to copy over and they all fit into capacity.
return Ok(());
};
// There are more items and there's not enough capacity to store them on the stack.
// Spill over to the heap and append the remaining items.
let mut segments = Vec::with_capacity(self.len * 2);
segments.extend_from_slice(self.as_slice());
segments.push(item);
segments.extend(iter);
Err(segments)
}
}
#[cfg(test)]
mod tests {
use crate::name::SegmentsVec;
#[test]
fn empty_vec() {
let empty = SegmentsVec::new();
assert_eq!(empty.as_slice(), &[] as &[&str]);
assert!(!empty.is_spilled());
}
#[test]
fn from_slice_stack() {
let stack = SegmentsVec::from_slice(&["a", "b", "c"]);
assert_eq!(stack.as_slice(), &["a", "b", "c"]);
assert!(!stack.is_spilled());
}
#[test]
fn from_slice_stack_capacity() {
let stack = SegmentsVec::from_slice(&["a", "b", "c", "d", "e", "f", "g", "h"]);
assert_eq!(stack.as_slice(), &["a", "b", "c", "d", "e", "f", "g", "h"]);
assert!(!stack.is_spilled());
}
#[test]
fn from_slice_heap() {
let heap = SegmentsVec::from_slice(&["a", "b", "c", "d", "e", "f", "g", "h", "i"]);
assert_eq!(
heap.as_slice(),
&["a", "b", "c", "d", "e", "f", "g", "h", "i"]
);
assert!(heap.is_spilled());
}
#[test]
fn push_stack() {
let mut stack = SegmentsVec::from_slice(&["a", "b", "c"]);
stack.push("d");
stack.push("e");
assert_eq!(stack.as_slice(), &["a", "b", "c", "d", "e"]);
assert!(!stack.is_spilled());
}
#[test]
fn push_stack_spill() {
let mut stack = SegmentsVec::from_slice(&["a", "b", "c", "d", "e", "f", "g"]);
stack.push("h");
assert!(!stack.is_spilled());
stack.push("i");
assert_eq!(
stack.as_slice(),
&["a", "b", "c", "d", "e", "f", "g", "h", "i"]
);
assert!(stack.is_spilled());
}
#[test]
fn pop_stack() {
let mut stack = SegmentsVec::from_slice(&["a", "b", "c", "d", "e"]);
assert_eq!(stack.pop(), Some("e"));
assert_eq!(stack.pop(), Some("d"));
assert_eq!(stack.pop(), Some("c"));
assert_eq!(stack.pop(), Some("b"));
assert_eq!(stack.pop(), Some("a"));
assert_eq!(stack.pop(), None);
assert!(!stack.is_spilled());
}
#[test]
fn pop_heap() {
let mut heap = SegmentsVec::from_slice(&["a", "b", "c", "d", "e", "f", "g", "h", "i"]);
assert_eq!(heap.pop(), Some("i"));
assert_eq!(heap.pop(), Some("h"));
assert_eq!(heap.pop(), Some("g"));
assert!(heap.is_spilled());
}
#[test]
fn extend_from_slice_stack() {
let mut stack = SegmentsVec::from_slice(&["a", "b", "c"]);
stack.extend_from_slice(&["d", "e", "f"]);
assert_eq!(stack.as_slice(), &["a", "b", "c", "d", "e", "f"]);
assert!(!stack.is_spilled());
}
#[test]
fn extend_from_slice_stack_spill() {
let mut spilled = SegmentsVec::from_slice(&["a", "b", "c", "d", "e", "f"]);
spilled.extend_from_slice(&["g", "h", "i", "j"]);
assert_eq!(
spilled.as_slice(),
&["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
);
assert!(spilled.is_spilled());
}
#[test]
fn extend_from_slice_heap() {
let mut heap = SegmentsVec::from_slice(&["a", "b", "c", "d", "e", "f", "g", "h", "i"]);
assert!(heap.is_spilled());
heap.extend_from_slice(&["j", "k", "l"]);
assert_eq!(
heap.as_slice(),
&["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"]
);
}
#[test]
fn extend_stack() {
let mut stack = SegmentsVec::from_slice(&["a", "b", "c"]);
stack.extend(["d", "e", "f"]);
assert_eq!(stack.as_slice(), &["a", "b", "c", "d", "e", "f"]);
assert!(!stack.is_spilled());
}
#[test]
fn extend_stack_spilled() {
let mut stack = SegmentsVec::from_slice(&["a", "b", "c", "d", "e", "f"]);
stack.extend(["g", "h", "i", "j"]);
assert_eq!(
stack.as_slice(),
&["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
);
assert!(stack.is_spilled());
}
#[test]
fn extend_heap() {
let mut heap = SegmentsVec::from_slice(&["a", "b", "c", "d", "e", "f", "g", "h", "i"]);
assert!(heap.is_spilled());
heap.extend(["j", "k", "l"]);
assert_eq!(
heap.as_slice(),
&["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"]
);
}
}