-- SPDX-FileCopyrightText: 2021 Tocqueville Group
--
-- SPDX-License-Identifier: LicenseRef-MIT-TQ
{-# LANGUAGE NoImplicitPrelude #-}
-- | Unsafe utilities.
--
-- This module should be imported qualified.
module Unsafe
( module Universum.Unsafe
-- * Unsafe converters between @Integral@ types checking for overflow/underflow
, Unsafe.fromIntegral
, Unsafe.fromInteger
) where
import Morley.Prelude.FromIntegral (fromIntegralNoOverflow)
import Universum
import Universum.Unsafe
-- | Unsafe converter between 'Integral' types
-- checking for overflow/underflow. Return
-- @value@ if conversion does not produce
-- overflow/underflow and raise an exception
-- with corresponding error message otherwise.
--
-- It is needed to replace 'Universum.Base.fromIntegral'
-- which misses most of the overflow/underflow checks.
--
-- Note the function is strict in its argument.
fromIntegral :: (HasCallStack, Integral a, Integral b) => a -> b
fromIntegral = either (error . fromString . displayException) id . fromIntegralNoOverflow
-- | Unsafe converter between 'Integer' and 'Integral'
-- types checking for overflow/underflow. Return @value@
-- if conversion does not produce overflow/underflow and
-- raise an exception with corresponding error message
-- otherwise.
--
-- Note the function is strict in its argument.
fromInteger :: (HasCallStack, Integral a) => Integer -> a
fromInteger = Unsafe.fromIntegral