| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | #![stable(feature = "rust1", since = "1.0.0")]
|
| 13 | |
| 14 | |
| 15 | #![cfg_attr(test, allow(unused_imports, dead_code))]
|
| 16 |
|
| 17 | use core::borrow::{Borrow, BorrowMut};
|
| 18 | #[cfg(not(no_global_oom_handling))]
|
| 19 | use core::cmp::Ordering::{self, Less};
|
| 20 | #[cfg(not(no_global_oom_handling))]
|
| 21 | use core::mem::{self, SizedTypeProperties};
|
| 22 | #[cfg(not(no_global_oom_handling))]
|
| 23 | use core::ptr;
|
| 24 | #[cfg(not(no_global_oom_handling))]
|
| 25 | use core::slice::sort;
|
| 26 |
|
| 27 | use crate::alloc::Allocator;
|
| 28 | #[cfg(not(no_global_oom_handling))]
|
| 29 | use crate::alloc::{self, Global};
|
| 30 | #[cfg(not(no_global_oom_handling))]
|
| 31 | use crate::borrow::ToOwned;
|
| 32 | use crate::boxed::Box;
|
| 33 | use crate::vec::Vec;
|
| 34 |
|
| 35 | #[cfg(test)]
|
| 36 | mod tests;
|
| 37 |
|
| 38 | #[unstable(feature = "slice_range", issue = "76393")]
|
| 39 | pub use core::slice::range;
|
| 40 | #[unstable(feature = "array_chunks", issue = "74985")]
|
| 41 | pub use core::slice::ArrayChunks;
|
| 42 | #[unstable(feature = "array_chunks", issue = "74985")]
|
| 43 | pub use core::slice::ArrayChunksMut;
|
| 44 | #[unstable(feature = "array_windows", issue = "75027")]
|
| 45 | pub use core::slice::ArrayWindows;
|
| 46 | #[stable(feature = "inherent_ascii_escape", since = "1.60.0")]
|
| 47 | pub use core::slice::EscapeAscii;
|
| 48 | #[stable(feature = "slice_get_slice", since = "1.28.0")]
|
| 49 | pub use core::slice::SliceIndex;
|
| 50 | #[stable(feature = "from_ref", since = "1.28.0")]
|
| 51 | pub use core::slice::{from_mut, from_ref};
|
| 52 | #[unstable(feature = "slice_from_ptr_range", issue = "89792")]
|
| 53 | pub use core::slice::{from_mut_ptr_range, from_ptr_range};
|
| 54 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 55 | pub use core::slice::{from_raw_parts, from_raw_parts_mut};
|
| 56 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 57 | pub use core::slice::{Chunks, Windows};
|
| 58 | #[stable(feature = "chunks_exact", since = "1.31.0")]
|
| 59 | pub use core::slice::{ChunksExact, ChunksExactMut};
|
| 60 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 61 | pub use core::slice::{ChunksMut, Split, SplitMut};
|
| 62 | #[unstable(feature = "slice_group_by", issue = "80552")]
|
| 63 | pub use core::slice::{GroupBy, GroupByMut};
|
| 64 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 65 | pub use core::slice::{Iter, IterMut};
|
| 66 | #[stable(feature = "rchunks", since = "1.31.0")]
|
| 67 | pub use core::slice::{RChunks, RChunksExact, RChunksExactMut, RChunksMut};
|
| 68 | #[stable(feature = "slice_rsplit", since = "1.27.0")]
|
| 69 | pub use core::slice::{RSplit, RSplitMut};
|
| 70 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 71 | pub use core::slice::{RSplitN, RSplitNMut, SplitN, SplitNMut};
|
| 72 | #[stable(feature = "split_inclusive", since = "1.51.0")]
|
| 73 | pub use core::slice::{SplitInclusive, SplitInclusiveMut};
|
| 74 |
|
| 75 | |
| 76 | |
| 77 | |
| 78 |
|
| 79 | |
| 80 | |
| 81 | #[cfg(test)]
|
| 82 | pub use hack::into_vec;
|
| 83 |
|
| 84 | |
| 85 | |
| 86 | #[cfg(test)]
|
| 87 | pub use hack::to_vec;
|
| 88 |
|
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | pub(crate) mod hack {
|
| 94 | use core::alloc::Allocator;
|
| 95 |
|
| 96 | use crate::boxed::Box;
|
| 97 | use crate::vec::Vec;
|
| 98 |
|
| 99 | |
| 100 | |
| 101 | |
| 102 | pub fn into_vec<T, A: Allocator>(b: Box<[T], A>) -> Vec<T, A> {
|
| 103 | unsafe {
|
| 104 | let len = b.len();
|
| 105 | let (b, alloc) = Box::into_raw_with_allocator(b);
|
| 106 | Vec::from_raw_parts_in(b as *mut T, len, len, alloc)
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | #[cfg(not(no_global_oom_handling))]
|
| 111 | #[inline]
|
| 112 | pub fn to_vec<T: ConvertVec, A: Allocator>(s: &[T], alloc: A) -> Vec<T, A> {
|
| 113 | T::to_vec(s, alloc)
|
| 114 | }
|
| 115 |
|
| 116 | #[cfg(not(no_global_oom_handling))]
|
| 117 | pub trait ConvertVec {
|
| 118 | fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A>
|
| 119 | where
|
| 120 | Self: Sized;
|
| 121 | }
|
| 122 |
|
| 123 | #[cfg(not(no_global_oom_handling))]
|
| 124 | impl<T: Clone> ConvertVec for T {
|
| 125 | #[inline]
|
| 126 | default fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> {
|
| 127 | struct DropGuard<'a, T, A: Allocator> {
|
| 128 | vec: &'a mut Vec<T, A>,
|
| 129 | num_init: usize,
|
| 130 | }
|
| 131 | impl<'a, T, A: Allocator> Drop for DropGuard<'a, T, A> {
|
| 132 | #[inline]
|
| 133 | fn drop(&mut self) {
|
| 134 | |
| 135 | |
| 136 | unsafe {
|
| 137 | self.vec.set_len(self.num_init);
|
| 138 | }
|
| 139 | }
|
| 140 | }
|
| 141 | let mut vec = Vec::with_capacity_in(s.len(), alloc);
|
| 142 | let mut guard = DropGuard { vec: &mut vec, num_init: 0 };
|
| 143 | let slots = guard.vec.spare_capacity_mut();
|
| 144 | |
| 145 | |
| 146 | for (i, b) in s.iter().enumerate().take(slots.len()) {
|
| 147 | guard.num_init = i;
|
| 148 | slots[i].write(b.clone());
|
| 149 | }
|
| 150 | core::mem::forget(guard);
|
| 151 | |
| 152 | |
| 153 | unsafe {
|
| 154 | vec.set_len(s.len());
|
| 155 | }
|
| 156 | vec
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | #[cfg(not(no_global_oom_handling))]
|
| 161 | impl<T: Copy> ConvertVec for T {
|
| 162 | #[inline]
|
| 163 | fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A> {
|
| 164 | let mut v = Vec::with_capacity_in(s.len(), alloc);
|
| 165 | |
| 166 | |
| 167 | |
| 168 | unsafe {
|
| 169 | s.as_ptr().copy_to_nonoverlapping(v.as_mut_ptr(), s.len());
|
| 170 | v.set_len(s.len());
|
| 171 | }
|
| 172 | v
|
| 173 | }
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | #[cfg(not(test))]
|
| 178 | impl<T> [T] {
|
| 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 | #[cfg(not(no_global_oom_handling))]
|
| 206 | #[rustc_allow_incoherent_impl]
|
| 207 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 208 | #[inline]
|
| 209 | pub fn sort(&mut self)
|
| 210 | where
|
| 211 | T: Ord,
|
| 212 | {
|
| 213 | stable_sort(self, T::lt);
|
| 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 | #[cfg(not(no_global_oom_handling))]
|
| 262 | #[rustc_allow_incoherent_impl]
|
| 263 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 264 | #[inline]
|
| 265 | pub fn sort_by<F>(&mut self, mut compare: F)
|
| 266 | where
|
| 267 | F: FnMut(&T, &T) -> Ordering,
|
| 268 | {
|
| 269 | stable_sort(self, |a, b| compare(a, b) == Less);
|
| 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 | #[cfg(not(no_global_oom_handling))]
|
| 304 | #[rustc_allow_incoherent_impl]
|
| 305 | #[stable(feature = "slice_sort_by_key", since = "1.7.0")]
|
| 306 | #[inline]
|
| 307 | pub fn sort_by_key<K, F>(&mut self, mut f: F)
|
| 308 | where
|
| 309 | F: FnMut(&T) -> K,
|
| 310 | K: Ord,
|
| 311 | {
|
| 312 | stable_sort(self, |a, b| f(a).lt(&f(b)));
|
| 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 | #[cfg(not(no_global_oom_handling))]
|
| 351 | #[rustc_allow_incoherent_impl]
|
| 352 | #[stable(feature = "slice_sort_by_cached_key", since = "1.34.0")]
|
| 353 | #[inline]
|
| 354 | pub fn sort_by_cached_key<K, F>(&mut self, f: F)
|
| 355 | where
|
| 356 | F: FnMut(&T) -> K,
|
| 357 | K: Ord,
|
| 358 | {
|
| 359 | |
| 360 | macro_rules! sort_by_key {
|
| 361 | ($t:ty, $slice:ident, $f:ident) => {{
|
| 362 | let mut indices: Vec<_> =
|
| 363 | $slice.iter().map($f).enumerate().map(|(i, k)| (k, i as $t)).collect();
|
| 364 | |
| 365 | |
| 366 | |
| 367 | indices.sort_unstable();
|
| 368 | for i in 0..$slice.len() {
|
| 369 | let mut index = indices[i].1;
|
| 370 | while (index as usize) < i {
|
| 371 | index = indices[index as usize].1;
|
| 372 | }
|
| 373 | indices[i].1 = index;
|
| 374 | $slice.swap(i, index as usize);
|
| 375 | }
|
| 376 | }};
|
| 377 | }
|
| 378 |
|
| 379 | let sz_u8 = mem::size_of::<(K, u8)>();
|
| 380 | let sz_u16 = mem::size_of::<(K, u16)>();
|
| 381 | let sz_u32 = mem::size_of::<(K, u32)>();
|
| 382 | let sz_usize = mem::size_of::<(K, usize)>();
|
| 383 |
|
| 384 | let len = self.len();
|
| 385 | if len < 2 {
|
| 386 | return;
|
| 387 | }
|
| 388 | if sz_u8 < sz_u16 && len <= (u8::MAX as usize) {
|
| 389 | return sort_by_key!(u8, self, f);
|
| 390 | }
|
| 391 | if sz_u16 < sz_u32 && len <= (u16::MAX as usize) {
|
| 392 | return sort_by_key!(u16, self, f);
|
| 393 | }
|
| 394 | if sz_u32 < sz_usize && len <= (u32::MAX as usize) {
|
| 395 | return sort_by_key!(u32, self, f);
|
| 396 | }
|
| 397 | sort_by_key!(usize, self, f)
|
| 398 | }
|
| 399 |
|
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | #[cfg(not(no_global_oom_handling))]
|
| 410 | #[rustc_allow_incoherent_impl]
|
| 411 | #[rustc_conversion_suggestion]
|
| 412 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 413 | #[inline]
|
| 414 | pub fn to_vec(&self) -> Vec<T>
|
| 415 | where
|
| 416 | T: Clone,
|
| 417 | {
|
| 418 | self.to_vec_in(Global)
|
| 419 | }
|
| 420 |
|
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | #[cfg(not(no_global_oom_handling))]
|
| 435 | #[rustc_allow_incoherent_impl]
|
| 436 | #[inline]
|
| 437 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 438 | pub fn to_vec_in<A: Allocator>(&self, alloc: A) -> Vec<T, A>
|
| 439 | where
|
| 440 | T: Clone,
|
| 441 | {
|
| 442 | |
| 443 | hack::to_vec(self, alloc)
|
| 444 | }
|
| 445 |
|
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | #[rustc_allow_incoherent_impl]
|
| 461 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 462 | #[inline]
|
| 463 | pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> {
|
| 464 | |
| 465 | hack::into_vec(self)
|
| 466 | }
|
| 467 |
|
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | #[rustc_allow_incoherent_impl]
|
| 489 | #[cfg(not(no_global_oom_handling))]
|
| 490 | #[stable(feature = "repeat_generic_slice", since = "1.40.0")]
|
| 491 | pub fn repeat(&self, n: usize) -> Vec<T>
|
| 492 | where
|
| 493 | T: Copy,
|
| 494 | {
|
| 495 | if n == 0 {
|
| 496 | return Vec::new();
|
| 497 | }
|
| 498 |
|
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 |
|
| 504 | |
| 505 | let capacity = self.len().checked_mul(n).expect("capacity overflow");
|
| 506 | let mut buf = Vec::with_capacity(capacity);
|
| 507 |
|
| 508 | |
| 509 | buf.extend(self);
|
| 510 | {
|
| 511 | let mut m = n >> 1;
|
| 512 | |
| 513 | while m > 0 {
|
| 514 | |
| 515 | unsafe {
|
| 516 | ptr::copy_nonoverlapping(
|
| 517 | buf.as_ptr(),
|
| 518 | (buf.as_mut_ptr() as *mut T).add(buf.len()),
|
| 519 | buf.len(),
|
| 520 | );
|
| 521 | |
| 522 | let buf_len = buf.len();
|
| 523 | buf.set_len(buf_len * 2);
|
| 524 | }
|
| 525 |
|
| 526 | m >>= 1;
|
| 527 | }
|
| 528 | }
|
| 529 |
|
| 530 | |
| 531 | |
| 532 | let rem_len = capacity - buf.len(); |
| 533 | if rem_len > 0 {
|
| 534 | |
| 535 | unsafe {
|
| 536 | |
| 537 | ptr::copy_nonoverlapping(
|
| 538 | buf.as_ptr(),
|
| 539 | (buf.as_mut_ptr() as *mut T).add(buf.len()),
|
| 540 | rem_len,
|
| 541 | );
|
| 542 | |
| 543 | buf.set_len(capacity);
|
| 544 | }
|
| 545 | }
|
| 546 | buf
|
| 547 | }
|
| 548 |
|
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | #[rustc_allow_incoherent_impl]
|
| 558 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 559 | pub fn concat<Item: ?Sized>(&self) -> <Self as Concat<Item>>::Output
|
| 560 | where
|
| 561 | Self: Concat<Item>,
|
| 562 | {
|
| 563 | Concat::concat(self)
|
| 564 | }
|
| 565 |
|
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | #[rustc_allow_incoherent_impl]
|
| 577 | #[stable(feature = "rename_connect_to_join", since = "1.3.0")]
|
| 578 | pub fn join<Separator>(&self, sep: Separator) -> <Self as Join<Separator>>::Output
|
| 579 | where
|
| 580 | Self: Join<Separator>,
|
| 581 | {
|
| 582 | Join::join(self, sep)
|
| 583 | }
|
| 584 |
|
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | #[rustc_allow_incoherent_impl]
|
| 596 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 597 | #[deprecated(since = "1.3.0", note = "renamed to join")]
|
| 598 | pub fn connect<Separator>(&self, sep: Separator) -> <Self as Join<Separator>>::Output
|
| 599 | where
|
| 600 | Self: Join<Separator>,
|
| 601 | {
|
| 602 | Join::join(self, sep)
|
| 603 | }
|
| 604 | }
|
| 605 |
|
| 606 | #[cfg(not(test))]
|
| 607 | impl [u8] {
|
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | #[cfg(not(no_global_oom_handling))]
|
| 618 | #[rustc_allow_incoherent_impl]
|
| 619 | #[must_use = "this returns the uppercase bytes as a new Vec, \
|
| 620 | without modifying the original"]
|
| 621 | #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
| 622 | #[inline]
|
| 623 | pub fn to_ascii_uppercase(&self) -> Vec<u8> {
|
| 624 | let mut me = self.to_vec();
|
| 625 | me.make_ascii_uppercase();
|
| 626 | me
|
| 627 | }
|
| 628 |
|
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | #[cfg(not(no_global_oom_handling))]
|
| 639 | #[rustc_allow_incoherent_impl]
|
| 640 | #[must_use = "this returns the lowercase bytes as a new Vec, \
|
| 641 | without modifying the original"]
|
| 642 | #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
|
| 643 | #[inline]
|
| 644 | pub fn to_ascii_lowercase(&self) -> Vec<u8> {
|
| 645 | let mut me = self.to_vec();
|
| 646 | me.make_ascii_lowercase();
|
| 647 | me
|
| 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 | #[unstable(feature = "slice_concat_trait", issue = "27747")]
|
| 685 | pub trait Concat<Item: ?Sized> {
|
| 686 | #[unstable(feature = "slice_concat_trait", issue = "27747")]
|
| 687 | |
| 688 | type Output;
|
| 689 |
|
| 690 | |
| 691 | #[unstable(feature = "slice_concat_trait", issue = "27747")]
|
| 692 | fn concat(slice: &Self) -> Self::Output;
|
| 693 | }
|
| 694 |
|
| 695 | |
| 696 | #[unstable(feature = "slice_concat_trait", issue = "27747")]
|
| 697 | pub trait Join<Separator> {
|
| 698 | #[unstable(feature = "slice_concat_trait", issue = "27747")]
|
| 699 | |
| 700 | type Output;
|
| 701 |
|
| 702 | |
| 703 | #[unstable(feature = "slice_concat_trait", issue = "27747")]
|
| 704 | fn join(slice: &Self, sep: Separator) -> Self::Output;
|
| 705 | }
|
| 706 |
|
| 707 | #[cfg(not(no_global_oom_handling))]
|
| 708 | #[unstable(feature = "slice_concat_ext", issue = "27747")]
|
| 709 | impl<T: Clone, V: Borrow<[T]>> Concat<T> for [V] {
|
| 710 | type Output = Vec<T>;
|
| 711 |
|
| 712 | fn concat(slice: &Self) -> Vec<T> {
|
| 713 | let size = slice.iter().map(|slice| slice.borrow().len()).sum();
|
| 714 | let mut result = Vec::with_capacity(size);
|
| 715 | for v in slice {
|
| 716 | result.extend_from_slice(v.borrow())
|
| 717 | }
|
| 718 | result
|
| 719 | }
|
| 720 | }
|
| 721 |
|
| 722 | #[cfg(not(no_global_oom_handling))]
|
| 723 | #[unstable(feature = "slice_concat_ext", issue = "27747")]
|
| 724 | impl<T: Clone, V: Borrow<[T]>> Join<&T> for [V] {
|
| 725 | type Output = Vec<T>;
|
| 726 |
|
| 727 | fn join(slice: &Self, sep: &T) -> Vec<T> {
|
| 728 | let mut iter = slice.iter();
|
| 729 | let first = match iter.next() {
|
| 730 | Some(first) => first,
|
| 731 | None => return vec![],
|
| 732 | };
|
| 733 | let size = slice.iter().map(|v| v.borrow().len()).sum::<usize>() + slice.len() - 1;
|
| 734 | let mut result = Vec::with_capacity(size);
|
| 735 | result.extend_from_slice(first.borrow());
|
| 736 |
|
| 737 | for v in iter {
|
| 738 | result.push(sep.clone());
|
| 739 | result.extend_from_slice(v.borrow())
|
| 740 | }
|
| 741 | result
|
| 742 | }
|
| 743 | }
|
| 744 |
|
| 745 | #[cfg(not(no_global_oom_handling))]
|
| 746 | #[unstable(feature = "slice_concat_ext", issue = "27747")]
|
| 747 | impl<T: Clone, V: Borrow<[T]>> Join<&[T]> for [V] {
|
| 748 | type Output = Vec<T>;
|
| 749 |
|
| 750 | fn join(slice: &Self, sep: &[T]) -> Vec<T> {
|
| 751 | let mut iter = slice.iter();
|
| 752 | let first = match iter.next() {
|
| 753 | Some(first) => first,
|
| 754 | None => return vec![],
|
| 755 | };
|
| 756 | let size =
|
| 757 | slice.iter().map(|v| v.borrow().len()).sum::<usize>() + sep.len() * (slice.len() - 1);
|
| 758 | let mut result = Vec::with_capacity(size);
|
| 759 | result.extend_from_slice(first.borrow());
|
| 760 |
|
| 761 | for v in iter {
|
| 762 | result.extend_from_slice(sep);
|
| 763 | result.extend_from_slice(v.borrow())
|
| 764 | }
|
| 765 | result
|
| 766 | }
|
| 767 | }
|
| 768 |
|
| 769 | |
| 770 | |
| 771 | |
| 772 |
|
| 773 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 774 | impl<T, A: Allocator> Borrow<[T]> for Vec<T, A> {
|
| 775 | fn borrow(&self) -> &[T] {
|
| 776 | &self[..]
|
| 777 | }
|
| 778 | }
|
| 779 |
|
| 780 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 781 | impl<T, A: Allocator> BorrowMut<[T]> for Vec<T, A> {
|
| 782 | fn borrow_mut(&mut self) -> &mut [T] {
|
| 783 | &mut self[..]
|
| 784 | }
|
| 785 | }
|
| 786 |
|
| 787 | |
| 788 | |
| 789 | |
| 790 | #[cfg(not(no_global_oom_handling))]
|
| 791 | pub(crate) trait SpecCloneIntoVec<T, A: Allocator> {
|
| 792 | fn clone_into(&self, target: &mut Vec<T, A>);
|
| 793 | }
|
| 794 |
|
| 795 | #[cfg(not(no_global_oom_handling))]
|
| 796 | impl<T: Clone, A: Allocator> SpecCloneIntoVec<T, A> for [T] {
|
| 797 | default fn clone_into(&self, target: &mut Vec<T, A>) {
|
| 798 | |
| 799 | target.truncate(self.len());
|
| 800 |
|
| 801 | |
| 802 | |
| 803 | let (init, tail) = self.split_at(target.len());
|
| 804 |
|
| 805 | |
| 806 | target.clone_from_slice(init);
|
| 807 | target.extend_from_slice(tail);
|
| 808 | }
|
| 809 | }
|
| 810 |
|
| 811 | #[cfg(not(no_global_oom_handling))]
|
| 812 | impl<T: Copy, A: Allocator> SpecCloneIntoVec<T, A> for [T] {
|
| 813 | fn clone_into(&self, target: &mut Vec<T, A>) {
|
| 814 | target.clear();
|
| 815 | target.extend_from_slice(self);
|
| 816 | }
|
| 817 | }
|
| 818 |
|
| 819 | #[cfg(not(no_global_oom_handling))]
|
| 820 | #[stable(feature = "rust1", since = "1.0.0")]
|
| 821 | impl<T: Clone> ToOwned for [T] {
|
| 822 | type Owned = Vec<T>;
|
| 823 | #[cfg(not(test))]
|
| 824 | fn to_owned(&self) -> Vec<T> {
|
| 825 | self.to_vec()
|
| 826 | }
|
| 827 |
|
| 828 | #[cfg(test)]
|
| 829 | fn to_owned(&self) -> Vec<T> {
|
| 830 | hack::to_vec(self, Global)
|
| 831 | }
|
| 832 |
|
| 833 | fn clone_into(&self, target: &mut Vec<T>) {
|
| 834 | SpecCloneIntoVec::clone_into(self, target);
|
| 835 | }
|
| 836 | }
|
| 837 |
|
| 838 | |
| 839 | |
| 840 | |
| 841 |
|
| 842 | #[inline]
|
| 843 | #[cfg(not(no_global_oom_handling))]
|
| 844 | fn stable_sort<T, F>(v: &mut [T], mut is_less: F)
|
| 845 | where
|
| 846 | F: FnMut(&T, &T) -> bool,
|
| 847 | {
|
| 848 | if T::IS_ZST {
|
| 849 | |
| 850 | return;
|
| 851 | }
|
| 852 |
|
| 853 | let elem_alloc_fn = |len: usize| -> *mut T {
|
| 854 | |
| 855 | |
| 856 | |
| 857 | unsafe { alloc::alloc(alloc::Layout::array::<T>(len).unwrap_unchecked()) as *mut T }
|
| 858 | };
|
| 859 |
|
| 860 | let elem_dealloc_fn = |buf_ptr: *mut T, len: usize| {
|
| 861 | |
| 862 | |
| 863 | |
| 864 | unsafe {
|
| 865 | alloc::dealloc(buf_ptr as *mut u8, alloc::Layout::array::<T>(len).unwrap_unchecked());
|
| 866 | }
|
| 867 | };
|
| 868 |
|
| 869 | let run_alloc_fn = |len: usize| -> *mut sort::TimSortRun {
|
| 870 | |
| 871 | |
| 872 | unsafe {
|
| 873 | alloc::alloc(alloc::Layout::array::<sort::TimSortRun>(len).unwrap_unchecked())
|
| 874 | as *mut sort::TimSortRun
|
| 875 | }
|
| 876 | };
|
| 877 |
|
| 878 | let run_dealloc_fn = |buf_ptr: *mut sort::TimSortRun, len: usize| {
|
| 879 | |
| 880 | |
| 881 | unsafe {
|
| 882 | alloc::dealloc(
|
| 883 | buf_ptr as *mut u8,
|
| 884 | alloc::Layout::array::<sort::TimSortRun>(len).unwrap_unchecked(),
|
| 885 | );
|
| 886 | }
|
| 887 | };
|
| 888 |
|
| 889 | sort::merge_sort(v, &mut is_less, elem_alloc_fn, elem_dealloc_fn, run_alloc_fn, run_dealloc_fn);
|
| 890 | }
|