sonic_rs/value/array.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 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
//! Represents a parsed JSON array. Its APIs are likes `Vec<Value>`.
use std::{
fmt::Debug,
iter::FusedIterator,
ops::{Deref, DerefMut, RangeBounds},
slice::{from_raw_parts, from_raw_parts_mut},
};
use super::node::ValueMut;
use crate::{
serde::tri,
value::{
node::{Value, ValueRefInner},
value_trait::JsonValueTrait,
},
};
/// Array represents a JSON array. Its APIs are likes `Array<Value>`.
///
/// # Example
/// ```
/// use sonic_rs::{array, Array, JsonContainerTrait};
///
/// let mut arr: Array = sonic_rs::from_str("[1, 2, 3]").unwrap();
/// assert_eq!(arr[0], 1);
///
/// let mut arr = array![1, 2, 3];
/// assert_eq!(arr[0], 1);
///
/// let j = sonic_rs::json!([1, 2, 3]);
/// assert_eq!(j.as_array().unwrap()[0], 1);
/// ```
#[derive(Debug, Eq, PartialEq, Clone)]
#[repr(transparent)]
pub struct Array(pub(crate) Value);
impl Array {
/// Returns the inner [`Value`].
#[inline]
pub fn into_value(self) -> Value {
self.0
}
/// Constructs a new, empty `Array`.
///
/// The array will not allocate until elements are pushed onto it.
///
/// # Example
/// ```
/// use sonic_rs::{array, from_str, json, prelude::*, Array};
///
/// let mut arr: Array = from_str("[]").unwrap();
/// dbg!(&arr);
/// arr.push(array![]);
/// arr.push(1);
/// arr[0] = "hello".into();
/// arr[1] = array![].into();
/// assert_eq!(arr[0], "hello");
/// assert_eq!(arr[1], array![]);
/// ```
#[inline]
pub const fn new() -> Self {
Array(Value::new_array())
}
/// Constructs a new, empty `Array` with at least the specified capacity.
///
/// The array will be able to hold at least `capacity` elements without
/// reallocating. This method is allowed to allocate for more elements than
/// `capacity`. If `capacity` is 0, the array will not allocate.
///
/// # Panics
///
/// Panics if the new capacity exceeds `isize::MAX` bytes.
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
let mut array = Self::new();
array.reserve(capacity);
array
}
/// Reserves capacity for at least `additional` more elements to be inserted
/// in the given `Array`. The collection may reserve more space to
/// speculatively avoid frequent reallocations. After calling `reserve`,
/// capacity will be greater than or equal to `self.len() + additional`.
/// Does nothing if capacity is already sufficient.
///
/// # Panics
///
/// Panics if the new capacity exceeds `isize::MAX` bytes.
///
/// # Examples
/// ```
/// use sonic_rs::array;
/// let mut arr = array![1, 2, 3];
/// arr.reserve(10);
/// assert!(arr.capacity() >= 13);
/// ```
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.0.reserve::<Value>(additional);
}
/// Resizes the `Array` in-place so that `len` is equal to `new_len`.
///
/// If `new_len` is greater than `len`, the `Array` is extended by the
/// difference, with each additional slot filled with the `Value` converted from `value`.
/// If `new_len` is less than `len`, the `Array` is simply truncated.
///
/// If you need more flexibility, use [`Array::resize_with`].
/// If you only need to resize to a smaller size, use [`Array::truncate`].
///
/// # Examples
///
/// ```
/// use sonic_rs::{array, json};
///
/// let mut arr = array!["hello"];
/// arr.resize(3, "world");
/// assert_eq!(arr, ["hello", "world", "world"]);
///
/// arr.resize(2, 0);
/// assert_eq!(arr, ["hello", "world"]);
///
/// arr.resize(4, json!("repeat"));
/// assert_eq!(arr, array!["hello", "world", "repeat", "repeat"]);
/// ```
#[inline]
pub fn resize<T: Clone + Into<Value>>(&mut self, new_len: usize, value: T) {
if new_len > self.len() {
self.reserve(new_len - self.len());
for _ in self.len()..new_len {
self.push(value.clone().into());
}
} else {
self.truncate(new_len);
}
}
/// Resizes the `Array` in-place so that `len` is equal to `new_len`.
///
/// If `new_len` is greater than `len`, the `Array` is extended by the
/// difference, with each additional slot filled with the result of
/// calling the closure `f`. The return values from `f` will end up
/// in the `Array` in the order they have been generated.
///
/// If `new_len` is less than `len`, the `Array` is simply truncated.
///
///
/// # Examples
///
/// ```
/// use sonic_rs::array;
/// let mut arr = array![1, 2, 3];
/// arr.resize_with(5, Default::default);
/// assert_eq!(arr, array![1, 2, 3, null, null]);
///
/// let mut arr = array![];
/// let mut p = 1;
/// arr.resize_with(4, || {
/// p *= 2;
/// p.into()
/// });
/// assert_eq!(arr, [2, 4, 8, 16]);
/// ```
#[inline]
pub fn resize_with<F>(&mut self, new_len: usize, mut f: F)
where
F: FnMut() -> Value,
{
if new_len > self.len() {
self.reserve(new_len - self.len());
for _ in self.len()..new_len {
self.push(f());
}
} else {
self.truncate(new_len);
}
}
/// Retains only the elements specified by the predicate.
///
/// In other words, remove all elements `e` for which `f(&e)` returns `false`.
/// This method operates in place, visiting each element exactly once in the
/// original order, and preserves the order of the retained elements.
///
/// Because the elements are visited exactly once in the original order,
/// external state may be used to decide which elements to keep.
///
/// # Examples
///
/// ```
/// use sonic_rs::array;
/// let mut arr = array![1, 2, 3, 4, 5];
/// let keep = [false, true, true, false, true];
/// let mut iter = keep.iter();
/// arr.retain(|_| *iter.next().unwrap());
/// assert_eq!(arr, array![2, 3, 5]);
/// ```
#[inline]
pub fn retain<F>(&mut self, mut f: F)
where
F: FnMut(&Value) -> bool,
{
self.retain_mut(|elem| f(elem));
}
/// Splits the collection into two at the given index.
///
/// Returns a newly allocated array containing the elements in the range
/// `[at, len)`. After the call, the original array will be left containing
/// the elements `[0, at)` with its previous capacity unchanged.
///
/// # Panics
///
/// Panics if `at > len`.
///
/// # Examples
///
/// ```
/// use sonic_rs::array;
/// let mut arr = array![1, 2, 3];
/// let arr2 = arr.split_off(1);
/// assert_eq!(arr, [1]);
/// assert_eq!(arr2, [2, 3]);
/// assert_eq!(arr.split_off(1), array![]);
/// ```
#[inline]
pub fn split_off(&mut self, at: usize) -> Self {
if let ValueMut::Array(array) = self.0.as_mut() {
array.split_off(at).into()
} else {
panic!("Array::split_off: not an array");
}
}
/// Removes an element from the array and returns it.
///
/// The removed element is replaced by the last element of the array.
///
/// This does not preserve ordering, but is *O*(1).
/// If you need to preserve the element order, use [`remove`] instead.
///
/// [`remove`]: Array::remove
///
/// # Panics
///
/// Panics if `index` is out of bounds.
///
/// # Examples
///
/// ```
/// use sonic_rs::array;
/// let mut v = array!["foo", "bar", "baz", "qux"];
///
/// assert_eq!(v.swap_remove(1), "bar");
/// assert_eq!(v, ["foo", "qux", "baz"]);
///
/// assert_eq!(v.swap_remove(0), "foo");
/// assert_eq!(v, ["baz", "qux"]);
/// ```
#[inline]
pub fn swap_remove(&mut self, index: usize) -> Value {
let len = self.len();
assert!(index < len, "index {} out of bounds(len: {})", index, len);
if index != self.len() - 1 {
unsafe {
let src = self.as_mut_ptr().add(index);
let dst = self.as_mut_ptr().add(len - 1);
std::ptr::swap(src, dst);
}
}
self.pop().unwrap()
}
/// Retains only the elements specified by the predicate, passing a mutable reference to it.
///
/// In other words, remove all elements `e` such that `f(&mut e)` returns `false`.
/// This method operates in place, visiting each element exactly once in the
/// original order, and preserves the order of the retained elements.
///
/// # Examples
///
/// ```
/// use sonic_rs::{array, JsonValueTrait};
///
/// let mut arr = array![1, 2, 3, 4];
/// arr.retain_mut(|x| {
/// let v = (x.as_i64().unwrap());
/// if v <= 3 {
/// *x = (v + 1).into();
/// true
/// } else {
/// false
/// }
/// });
/// assert_eq!(arr, [2, 3, 4]);
/// ```
#[inline]
pub fn retain_mut<F>(&mut self, f: F)
where
F: FnMut(&mut Value) -> bool,
{
if let ValueMut::Array(array) = self.0.as_mut() {
array.retain_mut(f);
} else {
panic!("Array::retain_mut: not an array");
}
}
/// Shortens the array, keeping the first `len` elements and dropping
/// the rest.
///
/// If `len` is greater or equal to the array's current length, this has
/// no effect.
///
/// The [`drain`] method can emulate `truncate`, but causes the excess
/// elements to be returned instead of dropped.
///
/// Note that this method has no effect on the allocated capacity
/// of the array.
///
/// # Examples
///
/// Truncating a five element array to two elements:
///
/// ```
/// use sonic_rs::array;
/// let mut arr = array![1, 2, 3, true, "hi"];
/// arr.truncate(2);
/// assert_eq!(arr, [1, 2]);
/// ```
///
/// No truncation occurs when `len` is greater than the array's current
/// length:
///
/// ```
/// use sonic_rs::array;
/// let mut arr = array![1, 2, 3];
/// arr.truncate(8);
/// assert_eq!(arr, [1, 2, 3]);
/// ```
///
/// Truncating when `len == 0` is equivalent to calling the [`clear`]
/// method.
///
/// ```
/// use sonic_rs::array;
/// let mut arr = array![1, 2, 3];
/// arr.truncate(0);
/// assert!(arr.is_empty());
/// ```
///
/// [`clear`]: Array::clear
/// [`drain`]: Array::drain
#[inline]
pub fn truncate(&mut self, len: usize) {
if let ValueMut::Array(array) = self.0.as_mut() {
array.truncate(len);
} else {
panic!("Array::truncate: not an array");
}
}
/// Appends an element `val` to the back of a collection.
/// The `val` will be converted into `Value`.
///
/// # Panics
///
/// Panics if the new capacity exceeds `isize::MAX` bytes.
///
/// # Examples
///
/// ```
/// use sonic_rs::array;
/// let mut arr = array![1, 2];
/// arr.push(3);
/// arr.push("hi");
/// assert_eq!(arr, array![1, 2, 3, "hi"]);
/// ```
#[inline]
pub fn push<T: Into<Value>>(&mut self, val: T) {
if let ValueMut::Array(array) = self.0.as_mut() {
array.push(val.into());
} else {
panic!("Array::push: not an array");
}
}
/// Removes the last element from a array and returns it, or [`None`] if it is empty.
#[inline]
pub fn pop(&mut self) -> Option<Value> {
debug_assert!(self.0.is_array());
self.0.pop()
}
/// Returns the number of elements in the array.
#[inline]
pub fn len(&self) -> usize {
self.0
.as_value_slice()
.expect("call len in non-array type")
.len()
}
/// Returns `true` if the array contains no elements.
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Extracts a mutable slice of the entire array. Equivalent to &mut s[..].
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [Value] {
self
}
/// Extracts a slice containing the entire array. Equivalent to &s[..].
#[inline]
pub fn as_slice(&self) -> &[Value] {
self
}
/// Returns the total number of elements the array can hold without reallocating.
#[inline]
pub fn capacity(&self) -> usize {
self.0.capacity()
}
/// Clears the array, removing all values.
///
/// Note that this method has no effect on the allocated capacity of the array.
#[inline]
pub fn clear(&mut self) {
self.0.clear();
}
/// Removes and returns the element at position `index` within the array,
///
/// # Panics
///
/// Panics if `index` is out of bounds.
///
/// # Examples
///
/// ```
/// use sonic_rs::array;
///
/// let mut arr = array![0, 1, 2];
/// arr.remove(1);
/// assert_eq!(arr, [0, 2]);
/// ```
#[inline]
pub fn remove(&mut self, index: usize) {
self.0.remove_index(index);
}
/// Moves all the elements of other into self, leaving other empty.
///
/// # Examples
/// ```
/// use sonic_rs::{array, Value};
///
/// let mut arr1 = array![1];
/// arr1.push(Value::from("arr1"));
///
/// let mut arr2 = array![2];
/// arr2.push(Value::from("arr2"));
/// arr2.append(&mut arr1);
///
/// assert_eq!(arr2, array![2, "arr2", 1, "arr1"]);
/// assert!(arr1.is_empty());
/// ```
#[inline]
pub fn append(&mut self, other: &mut Self) {
if let ValueMut::Array(array) = self.0.as_mut() {
if let ValueMut::Array(other_array) = other.0.as_mut() {
array.append(other_array);
} else {
panic!("Array::append: other is not an array");
}
} else {
panic!("Array::append: not an array");
}
}
/// Removes the specified range from the array in bulk, returning all
/// removed elements as an iterator. If the iterator is dropped before
/// being fully consumed, it drops the remaining removed elements.
///
/// The returned iterator keeps a mutable borrow on the array to optimize
/// its implementation.
///
/// # Panics
///
/// Panics if the starting point is greater than the end point or if
/// the end point is greater than the length of the array.
///
/// # Leaking
///
/// If the returned iterator goes out of scope without being dropped (due to
/// [`std::mem::forget`], for example), the array may have lost and leaked
/// elements arbitrarily, including elements outside the range.
///
/// # Examples
///
/// ```
/// use sonic_rs::{array, Value};
/// let mut v = array![1, 2, 3];
/// let u: Vec<Value> = v.drain(1..).collect();
/// assert_eq!(v, &[1]);
/// assert_eq!(u, &[2, 3]);
///
/// // A full range clears the array, like `clear()` does
/// v.drain(..);
/// assert!(v.is_empty());
/// ```
#[inline]
pub fn drain<R>(&mut self, r: R) -> Drain<'_>
where
R: RangeBounds<usize>,
{
if let ValueMut::Array(array) = self.0.as_mut() {
array.drain(r)
} else {
panic!("Array::drain: not an array");
}
}
/// Copies elements from `src` range to the end of the array.
///
/// # Panics
///
/// Panics if the starting point is greater than the end point or if
/// the end point is greater than the length of the array.
///
/// # Examples
///
/// ```
/// use sonic_rs::Array;
/// let mut arr: Array = sonic_rs::from_str("[0, 1, 2, 3, 4]").unwrap();
///
/// arr.extend_from_within(2..);
/// assert_eq!(arr, [0, 1, 2, 3, 4, 2, 3, 4]);
///
/// arr.extend_from_within(..2);
/// assert_eq!(arr, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);
///
/// arr.extend_from_within(4..8);
/// assert_eq!(arr, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);
/// ```
pub fn extend_from_within<R>(&mut self, src: R)
where
R: RangeBounds<usize>,
{
if let ValueMut::Array(array) = self.0.as_mut() {
array.extend_from_within(src);
} else {
panic!("Array::extend_from_within: not an array");
}
}
/// Inserts an element at position `index` within the array, shifting all
/// elements after it to the right.
/// The `element` will be converted into `Value`.
///
/// # Panics
///
/// Panics if `index > len`.
///
/// # Examples
///
/// ```
/// use sonic_rs::{array, json};
///
/// let mut arr = array![1, 2, 3];
/// arr.insert(1, "inserted1");
/// assert_eq!(arr, array![1, "inserted1", 2, 3]);
///
/// arr.insert(4, "inserted2");
/// assert_eq!(arr, array![1, "inserted1", 2, 3, "inserted2"]);
///
/// arr.insert(5, json!({"a": 123})); // insert at the end
/// assert_eq!(arr, array![1, "inserted1", 2, 3, "inserted2", {"a": 123}]);
/// ```
#[inline]
pub fn insert<T: Into<Value>>(&mut self, index: usize, element: T) {
if let ValueMut::Array(array) = self.0.as_mut() {
array.insert(index, element.into());
} else {
panic!("Array::insert: not an array");
}
}
}
impl Default for Array {
fn default() -> Self {
Self::new()
}
}
impl Deref for Array {
type Target = [Value];
fn deref(&self) -> &Self::Target {
self.0.as_value_slice().expect("Array::deref: not an array")
}
}
impl DerefMut for Array {
fn deref_mut(&mut self) -> &mut Self::Target {
if let ValueMut::Array(array) = self.0.as_mut() {
array
} else {
panic!("Array::deref_mut: not an array");
}
}
}
/// A draining iterator for `Array<T>`.
///
/// This `struct` is created by [`Array::drain`].
/// See its documentation for more.
pub type Drain<'a> = std::vec::Drain<'a, Value>;
use std::{
ops::{Index, IndexMut},
slice::SliceIndex,
};
impl<I: SliceIndex<[Value]>> Index<I> for Array {
type Output = I::Output;
#[inline]
fn index(&self, index: I) -> &Self::Output {
Index::index(&**self, index)
}
}
impl<I: SliceIndex<[Value]>> IndexMut<I> for Array {
fn index_mut(&mut self, index: I) -> &mut Self::Output {
IndexMut::index_mut(&mut **self, index)
}
}
//////////////////////////////////////////////////////////////////////////////
#[derive(Debug, Default, Clone)]
pub struct IntoIter {
array: Array,
index: usize,
len: usize,
}
impl IntoIter {
pub fn as_mut_slice(&mut self) -> &mut [Value] {
if let ValueMut::Array(array) = self.array.0.as_mut() {
unsafe {
let ptr = array.as_mut_ptr();
let len = array.len();
from_raw_parts_mut(ptr, len)
}
} else {
panic!("Array::as_mut_slice: not an array");
}
}
pub fn as_slice(&self) -> &[Value] {
if let ValueRefInner::Array(array) = self.array.0.as_ref2() {
unsafe {
let ptr = array.as_ptr();
let len = array.len();
from_raw_parts(ptr, len)
}
} else {
panic!("Array::as_slice: not an array");
}
}
}
impl AsRef<[Value]> for IntoIter {
fn as_ref(&self) -> &[Value] {
self.as_slice()
}
}
impl AsMut<[Value]> for IntoIter {
fn as_mut(&mut self) -> &mut [Value] {
self.as_mut_slice()
}
}
impl DoubleEndedIterator for IntoIter {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
if self.index < self.len {
self.len -= 1;
let value = self.array.0.get_index_mut(self.len).unwrap();
Some(value.take())
} else {
None
}
}
}
impl ExactSizeIterator for IntoIter {
#[inline]
fn len(&self) -> usize {
self.len - self.index
}
}
impl FusedIterator for IntoIter {}
impl Iterator for IntoIter {
type Item = Value;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.len {
let value = self.array.0.get_index_mut(self.index).unwrap();
self.index += 1;
Some(value.take())
} else {
None
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len - self.index;
(len, Some(len))
}
}
impl IntoIterator for Array {
type Item = Value;
type IntoIter = IntoIter;
#[inline]
fn into_iter(self) -> Self::IntoIter {
let len = self.len();
IntoIter {
array: self,
index: 0,
len,
}
}
}
impl<'a> IntoIterator for &'a Array {
type Item = &'a Value;
type IntoIter = std::slice::Iter<'a, Value>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a> IntoIterator for &'a mut Array {
type Item = &'a mut Value;
type IntoIter = std::slice::IterMut<'a, Value>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
//////////////////////////////////////////////////////////////////////////////
impl serde::ser::Serialize for Array {
#[inline]
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
use serde::ser::SerializeSeq;
let mut seq = tri!(serializer.serialize_seq(Some(self.len())));
for v in self {
tri!(seq.serialize_element(v));
}
seq.end()
}
}
impl<'de> serde::de::Deserialize<'de> for Array {
#[inline]
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{
// deserialize to value at first
let value: Value =
deserializer.deserialize_newtype_struct(super::de::TOKEN, super::de::ValueVisitor)?;
if value.is_array() {
Ok(Array(value))
} else {
Err(serde::de::Error::invalid_type(
serde::de::Unexpected::Other("not a array"),
&"array",
))
}
}
}
#[cfg(test)]
mod test {
use super::Array;
use crate::value::{node::Value, value_trait::JsonValueMutTrait};
#[test]
fn test_value_array() {
let mut val = crate::from_str::<Value>(r#"[1,2,3]"#).unwrap();
let array = val.as_array_mut().unwrap();
assert_eq!(array.len(), 3);
for i in 0..3 {
// push static node
let old_len = array.len();
let new_node = Value::new_u64(i);
array.push(new_node);
assert_eq!(array.len(), old_len + 1);
// push node with new allocator
let old_len = array.len();
let mut new_node = Array::default();
new_node.push(Value::new_u64(i));
array.push(new_node.0);
assert_eq!(array.len(), old_len + 1);
// push node with self allocator
let old_len = array.len();
let mut new_node = Array::new();
new_node.push(Value::new_u64(i));
array.push(new_node.0);
assert_eq!(array.len(), old_len + 1);
}
for (i, v) in array.iter_mut().enumerate() {
*v = Value::new_u64(i as u64);
}
while !array.is_empty() {
dbg!(array.pop());
}
}
}