Skip to main content

arrow_buffer/
error.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Errors returned by `arrow-buffer`.
19
20/// An arithmetic overflow.
21///
22/// Returned by the `try_` alternatives to the functions that panic on overflow,
23/// for instance [`OffsetBuffer::try_from_lengths`](crate::OffsetBuffer::try_from_lengths).
24///
25/// ```
26/// # use arrow_buffer::OffsetBuffer;
27/// // 32 bit offsets cannot describe more than 2 GiB of data:
28/// let err = OffsetBuffer::<i32>::try_from_lengths([u32::MAX as usize]).unwrap_err();
29/// assert_eq!(err.to_string(), "offset overflow: 4294967295 does not fit in i32");
30///
31/// // 64 bit offsets can:
32/// assert!(OffsetBuffer::<i64>::try_from_lengths([u32::MAX as usize]).is_ok());
33/// ```
34#[derive(Clone, Copy, Debug, PartialEq, Eq)]
35pub struct OverflowError {
36    what: &'static str,
37    value: Option<usize>,
38    type_name: &'static str,
39}
40
41impl OverflowError {
42    /// `what` names what overflowed, for instance `"offset"`,
43    /// and `T` is the type it did not fit in, for instance `i32`.
44    pub fn new<T>(what: &'static str) -> Self {
45        Self {
46            what,
47            value: None,
48            type_name: std::any::type_name::<T>(),
49        }
50    }
51
52    /// The value that did not fit.
53    pub const fn with_value(mut self, value: usize) -> Self {
54        self.value = Some(value);
55        self
56    }
57
58    /// What overflowed, for instance `"offset"`.
59    pub const fn what(&self) -> &'static str {
60        self.what
61    }
62
63    /// The value that did not fit, if known.
64    pub const fn value(&self) -> Option<usize> {
65        self.value
66    }
67
68    /// The name of the type that the value did not fit in, for instance `"i32"`.
69    pub const fn type_name(&self) -> &'static str {
70        self.type_name
71    }
72}
73
74impl std::fmt::Display for OverflowError {
75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        let Self {
77            what,
78            value,
79            type_name,
80        } = self;
81        write!(f, "{what} overflow: ")?;
82        match value {
83            Some(value) => write!(f, "{value} does not fit in {type_name}"),
84            None => write!(f, "does not fit in {type_name}"),
85        }
86    }
87}
88
89impl std::error::Error for OverflowError {}