Skip to main content

arrow_buffer/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Errors returned by `arrow-buffer`.
6
7/// An arithmetic overflow.
8///
9/// Returned by the `try_` alternatives to the functions that panic on overflow,
10/// for instance [`OffsetBuffer::try_from_lengths`](crate::OffsetBuffer::try_from_lengths).
11///
12/// ```
13/// # use arrow_buffer::OffsetBuffer;
14/// // 32 bit offsets cannot describe more than 2 GiB of data:
15/// let err = OffsetBuffer::<i32>::try_from_lengths([u32::MAX as usize]).unwrap_err();
16/// assert_eq!(err.to_string(), "offset overflow: 4294967295 does not fit in i32");
17///
18/// // 64 bit offsets can:
19/// assert!(OffsetBuffer::<i64>::try_from_lengths([u32::MAX as usize]).is_ok());
20/// ```
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub struct OverflowError {
23    what: &'static str,
24    value: Option<usize>,
25    type_name: &'static str,
26}
27
28impl OverflowError {
29    /// `what` names what overflowed, for instance `"offset"`,
30    /// and `T` is the type it did not fit in, for instance `i32`.
31    pub fn new<T>(what: &'static str) -> Self {
32        Self {
33            what,
34            value: None,
35            type_name: std::any::type_name::<T>(),
36        }
37    }
38
39    /// The value that did not fit.
40    pub const fn with_value(mut self, value: usize) -> Self {
41        self.value = Some(value);
42        self
43    }
44
45    /// What overflowed, for instance `"offset"`.
46    pub const fn what(&self) -> &'static str {
47        self.what
48    }
49
50    /// The value that did not fit, if known.
51    pub const fn value(&self) -> Option<usize> {
52        self.value
53    }
54
55    /// The name of the type that the value did not fit in, for instance `"i32"`.
56    pub const fn type_name(&self) -> &'static str {
57        self.type_name
58    }
59}
60
61impl std::fmt::Display for OverflowError {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        let Self {
64            what,
65            value,
66            type_name,
67        } = self;
68        write!(f, "{what} overflow: ")?;
69        match value {
70            Some(value) => write!(f, "{value} does not fit in {type_name}"),
71            None => write!(f, "does not fit in {type_name}"),
72        }
73    }
74}
75
76impl std::error::Error for OverflowError {}
77
78/// A slice that does not fit in the buffer it is taken from.
79///
80/// Returned by [`Buffer::try_slice_with_length`](crate::Buffer::try_slice_with_length),
81/// the alternative to [`Buffer::slice_with_length`](crate::Buffer::slice_with_length)
82/// that does not panic.
83#[derive(Clone, Copy, Debug, PartialEq, Eq)]
84pub struct SliceOutOfBoundsError {
85    offset: usize,
86    length: usize,
87    buffer_len: usize,
88}
89
90impl SliceOutOfBoundsError {
91    pub(crate) const fn new(offset: usize, length: usize, buffer_len: usize) -> Self {
92        Self {
93            offset,
94            length,
95            buffer_len,
96        }
97    }
98}
99
100impl std::fmt::Display for SliceOutOfBoundsError {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        let Self {
103            offset,
104            length,
105            buffer_len,
106        } = self;
107        write!(
108            f,
109            "the offset of the new Buffer cannot exceed the existing length: slice offset={offset} length={length} selflen={buffer_len}"
110        )
111    }
112}
113
114impl std::error::Error for SliceOutOfBoundsError {}