malachite_nz/integer_vector/conversion/from_natural_vector.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8use crate::integer::Integer;
9use crate::integer_vector::IntegerVector;
10use crate::natural_vector::NaturalVector;
11
12impl From<NaturalVector> for IntegerVector {
13 /// Converts a [`NaturalVector`] to an [`IntegerVector`].
14 ///
15 /// Every [`Natural`](crate::natural::Natural) is an [`Integer`], so nothing is lost and nothing
16 /// can fail. The elements are converted one by one, without copying their limbs, so the
17 /// dimension is unchanged.
18 ///
19 /// # Worst-case complexity
20 /// $T(n) = O(n)$
21 ///
22 /// $M(n) = O(n)$
23 ///
24 /// where $T$ is time, $M$ is additional memory, and $n$ is `v.dimension()`.
25 ///
26 /// # Examples
27 /// ```
28 /// use core::str::FromStr;
29 /// use malachite_nz::integer_vector::IntegerVector;
30 /// use malachite_nz::natural_vector::NaturalVector;
31 ///
32 /// let v = NaturalVector::from_str("(1, 2, 3)").unwrap();
33 /// assert_eq!(IntegerVector::from(v).to_string(), "(1, 2, 3)");
34 ///
35 /// assert_eq!(
36 /// IntegerVector::from(NaturalVector::from_str("()").unwrap()).to_string(),
37 /// "()"
38 /// );
39 /// ```
40 #[inline]
41 fn from(v: NaturalVector) -> Self {
42 Self {
43 elements: v.elements.into_iter().map(Integer::from).collect(),
44 }
45 }
46}