Skip to main content

malachite_nz/gaussian_integer/conversion/
from.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/>.
8
9use crate::gaussian_integer::GaussianInteger;
10use crate::integer::Integer;
11use malachite_base::num::basic::traits::Zero;
12
13impl<T> From<T> for GaussianInteger
14where
15    Integer: From<T>,
16{
17    /// Converts a value of any type that converts to an [`Integer`] — including [`Integer`]
18    /// itself, via the standard library's reflexive [`From`] — to a purely real
19    /// [`GaussianInteger`].
20    ///
21    /// # Worst-case complexity
22    /// Same as the complexity of the corresponding [`Integer`] conversion.
23    ///
24    /// # Examples
25    /// ```
26    /// use malachite_nz::gaussian_integer::GaussianInteger;
27    /// use malachite_nz::integer::Integer;
28    /// use malachite_nz::natural::Natural;
29    ///
30    /// assert_eq!(GaussianInteger::from(123u32).to_string(), "123");
31    /// assert_eq!(GaussianInteger::from(-123i64).to_string(), "-123");
32    /// assert_eq!(
33    ///     GaussianInteger::from(Natural::from(123u32)).to_string(),
34    ///     "123"
35    /// );
36    /// assert_eq!(
37    ///     GaussianInteger::from(Integer::from(-123)).to_string(),
38    ///     "-123"
39    /// );
40    /// ```
41    fn from(x: T) -> Self {
42        Self {
43            real: Integer::from(x),
44            imaginary: Integer::ZERO,
45        }
46    }
47}