Files
dioxus/packages/generational-box/src/error.rs
T
Nico Burns 9359446017 Upgrade to Blitz v0.2.0 (#4731)
* Bump Rust CI version to 1.88

Signed-off-by: Nico Burns <nico@nicoburns.com>

* Upgrade to Blitz v0.2.0

Signed-off-by: Nico Burns <nico@nicoburns.com>

* Native: enable clipboard and file_dialog support by default

* Fix clippy errors

Signed-off-by: Nico Burns <nico@nicoburns.com>

* See if cargo update magically fixes build

Signed-off-by: Nico Burns <nico@nicoburns.com>

* Fix compile err

Signed-off-by: Nico Burns <nico@nicoburns.com>

* Remove unused wasm-opt dep

* Fix wgpu_child_window example

Signed-off-by: Nico Burns <nico@nicoburns.com>

* Update blitz-shell

Signed-off-by: Nico Burns <nico@nicoburns.com>

* cargo fmt

Signed-off-by: Nico Burns <nico@nicoburns.com>

* Allow clippy lint

Signed-off-by: Nico Burns <nico@nicoburns.com>

* CI: Bump pinned nightly version

Signed-off-by: Nico Burns <nico@nicoburns.com>

* Allow clippy lint in whole module

Signed-off-by: Nico Burns <nico@nicoburns.com>

* Fix more clippy lints

Signed-off-by: Nico Burns <nico@nicoburns.com>

* Use Rust 1.90 for clippy

Signed-off-by: Nico Burns <nico@nicoburns.com>

* cargo update --ignore-rust-version

* revert cargo updates

Signed-off-by: Nico Burns <nico@nicoburns.com>

* Revert another silly clippy lint

---------

Signed-off-by: Nico Burns <nico@nicoburns.com>
2025-10-07 11:18:59 -07:00

167 lines
5.6 KiB
Rust

//! Generational box errors
#![allow(clippy::uninlined_format_args, reason = "causes compile error")]
use std::error::Error;
use std::fmt::Debug;
use std::fmt::Display;
use crate::GenerationalLocation;
/// A result that can be returned from a borrow operation.
pub type BorrowResult<T = ()> = std::result::Result<T, BorrowError>;
/// A result that can be returned from a borrow mut operation.
pub type BorrowMutResult<T = ()> = std::result::Result<T, BorrowMutError>;
#[derive(Debug, Clone, PartialEq)]
/// An error that can occur when trying to borrow a value.
pub enum BorrowError {
/// The value was dropped.
Dropped(ValueDroppedError),
/// The value was already borrowed mutably.
AlreadyBorrowedMut(AlreadyBorrowedMutError),
}
impl Display for BorrowError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BorrowError::Dropped(error) => Display::fmt(error, f),
BorrowError::AlreadyBorrowedMut(error) => Display::fmt(error, f),
}
}
}
impl Error for BorrowError {}
#[derive(Debug, Clone, PartialEq)]
/// An error that can occur when trying to borrow a value mutably.
pub enum BorrowMutError {
/// The value was dropped.
Dropped(ValueDroppedError),
/// The value was already borrowed.
AlreadyBorrowed(AlreadyBorrowedError),
/// The value was already borrowed mutably.
AlreadyBorrowedMut(AlreadyBorrowedMutError),
}
impl Display for BorrowMutError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BorrowMutError::Dropped(error) => Display::fmt(error, f),
BorrowMutError::AlreadyBorrowedMut(error) => Display::fmt(error, f),
BorrowMutError::AlreadyBorrowed(error) => Display::fmt(error, f),
}
}
}
impl Error for BorrowMutError {}
impl From<BorrowError> for BorrowMutError {
fn from(error: BorrowError) -> Self {
match error {
BorrowError::Dropped(error) => BorrowMutError::Dropped(error),
BorrowError::AlreadyBorrowedMut(error) => BorrowMutError::AlreadyBorrowedMut(error),
}
}
}
/// An error that can occur when trying to use a value that has been dropped.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct ValueDroppedError {
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
pub(crate) created_at: &'static std::panic::Location<'static>,
}
impl ValueDroppedError {
/// Create a new `ValueDroppedError`.
#[allow(unused)]
pub fn new(created_at: &'static std::panic::Location<'static>) -> Self {
Self {
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
created_at,
}
}
/// Create a new `ValueDroppedError` for a [`GenerationalLocation`].
#[allow(unused)]
pub(crate) fn new_for_location(location: GenerationalLocation) -> Self {
Self {
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
created_at: location.created_at,
}
}
}
impl Display for ValueDroppedError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Failed to borrow because the value was dropped.")?;
#[cfg(any(debug_assertions, feature = "debug_ownership"))]
f.write_fmt(format_args!("created_at: {}", self.created_at))?;
Ok(())
}
}
impl std::error::Error for ValueDroppedError {}
/// An error that can occur when trying to borrow a value that has already been borrowed mutably.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct AlreadyBorrowedMutError {
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
pub(crate) borrowed_mut_at: &'static std::panic::Location<'static>,
}
impl AlreadyBorrowedMutError {
/// Create a new `AlreadyBorrowedMutError`.
#[allow(unused)]
pub fn new(borrowed_mut_at: &'static std::panic::Location<'static>) -> Self {
Self {
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrowed_mut_at,
}
}
}
impl Display for AlreadyBorrowedMutError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Failed to borrow because the value was already borrowed mutably.")?;
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
f.write_fmt(format_args!("borrowed_mut_at: {}", self.borrowed_mut_at))?;
Ok(())
}
}
impl std::error::Error for AlreadyBorrowedMutError {}
/// An error that can occur when trying to borrow a value mutably that has already been borrowed immutably.
#[derive(Debug, Clone, PartialEq)]
pub struct AlreadyBorrowedError {
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
pub(crate) borrowed_at: Vec<&'static std::panic::Location<'static>>,
}
impl AlreadyBorrowedError {
/// Create a new `AlreadyBorrowedError`.
#[allow(unused)]
pub fn new(borrowed_at: Vec<&'static std::panic::Location<'static>>) -> Self {
Self {
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
borrowed_at,
}
}
}
impl Display for AlreadyBorrowedError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Failed to borrow mutably because the value was already borrowed immutably.")?;
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
f.write_str("borrowed_at:")?;
#[cfg(any(debug_assertions, feature = "debug_borrows"))]
for location in self.borrowed_at.iter() {
f.write_fmt(format_args!("\t{}", location))?;
}
Ok(())
}
}
impl std::error::Error for AlreadyBorrowedError {}