code: def range_to_ndarray(rng: range) -> np.ndarray:
      """
      Cast a range object to ndarray.
      """
      # GH#30171 perf avoid realizing range as a list in np.array
      try:
          arr = np.arange(rng.start, rng.stop, rng.step, dtype="int64")
      except OverflowError:
          # GH#30173 handling for ranges that overflow int64
          if (rng.start >= 0 and rng.step > 0) or (
              rng.step < 0 <= rng.stop
          ):
              try:
                  arr = np.arange(
                      rng.start, rng.stop, rng.step, dtype="uint64"
                  )
              except OverflowError:
                  arr = construct_1d_object_array_from_listlike(
                      list(rng)
                  )
          else:
              arr = construct_1d_object_array_from_listlike(list(rng))
      return arr
  
imports: from __future__ import annotations
  from typing import TYPE_CHECKING, Optional, Sequence, Union, cast, overload
  import numpy
  from numpy import ma
  from pandas._libs import lib
  from pandas._libs.tslibs.period import Period
  from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, T
  from pandas.core.dtypes.base import ExtensionDtype, _registry
  from pandas.core.dtypes.cast import construct_1d_arraylike_from_scalar, construct_1d_object_array_from_listlike, maybe_cast_to_datetime, maybe_cast_to_integer_array, maybe_convert_platform, maybe_infer_to_datetimelike, maybe_promote
  from pandas.core.dtypes.common import is_datetime64_ns_dtype, is_dtype_equal, is_extension_array_dtype, is_integer_dtype, is_list_like, is_object_dtype, is_timedelta64_ns_dtype
  from pandas.core.dtypes.dtypes import PandasDtype
  from pandas.core.dtypes.generic import ABCDataFrame, ABCExtensionArray, ABCIndex, ABCPandasArray, ABCRangeIndex, ABCSeries
  from pandas.core.dtypes.missing import isna
  import pandas.core.common
label_desc: Use image node_id 8 for calling a global function with example usage: range_to_ndarray(rng) and returns: arr