Skip to content

Commit 57ec46a

Browse files
committed
🧪 Using error type pointer
1 parent 0d8cb7d commit 57ec46a

File tree

7 files changed

+63
-9
lines changed

7 files changed

+63
-9
lines changed

‎Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ exclude = [
88
"projects/.DS_Store",
99
]
1010

11+
[workspace.dependencies]
12+
1113
[profile.release]
1214
lto = true
1315
panic = "abort"

‎projects/sub_projects/Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ publish = false
44
version = "0.0.0"
55
authors = ["Aster <192607617@qq.com>"]
66
description = "..."
7-
repository = "https://github.com/oovm/sub_projects"
7+
categories = ["rust-patterns"]
8+
homepage = "https://github.com/oovm/RustTemplate"
9+
repository = "https://github.com/oovm/RustTemplate"
810
documentation = "https://docs.rs/sub_projects"
9-
readme = "Readme.md"
11+
readme = "readme.md"
1012
license = "MPL-2.0"
1113
edition = "2021"
1214
exclude = ["package.json", "tests/**"]

‎projects/sub_projects/src/errors.rs

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use super::*;
2+
3+
impl From<ExampleErrorKind> for ExampleError {
4+
fn from(value: ExampleErrorKind) -> Self {
5+
Self {
6+
kind: Box::new(value),
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use super::*;
2+
3+
impl Error for ExampleError {}
4+
5+
6+
impl Debug for ExampleError {
7+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
8+
Debug::fmt(&self.kind, f)
9+
}
10+
}
11+
12+
impl Display for ExampleError {
13+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
14+
Display::fmt(&self.kind, f)
15+
}
16+
}
17+
18+
19+
impl Display for ExampleErrorKind {
20+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21+
match self { ExampleErrorKind::UnknownError => { write!(f, "UnknownError") } }
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::fmt::{Debug, Formatter};
2+
use std::error::Error;
3+
use std::fmt::Display;
4+
5+
mod display;
6+
mod convert;
7+
8+
/// The result type of this crate.
9+
pub type Result<T> = std::result::Result<T, ExampleError>;
10+
11+
/// A boxed error kind, wrapping an [ExampleErrorKind].
12+
#[derive(Clone)]
13+
pub struct ExampleError {
14+
kind: Box<ExampleErrorKind>,
15+
}
16+
17+
/// The kind of [ExampleError].
18+
#[derive(Debug, Copy, Clone)]
19+
pub enum ExampleErrorKind {
20+
/// An unknown error.
21+
UnknownError
22+
}
23+
24+

‎projects/sub_projects/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
mod errors;
88

9-
pub use crate::errors::{Error, Result};
9+
pub use crate::errors::{ExampleErrorKind, Result, ExampleError};

0 commit comments

Comments
 (0)