Skip to content

Commit

Permalink
update changelog. (#911)
Browse files Browse the repository at this point in the history
* update changelog.

* update test dep.

* add example for trait object as application state.

* restructure example.
  • Loading branch information
fakeshadow authored Jan 24, 2024
1 parent c9f07a7 commit ecc2fdf
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"cloudflare-worker",
"compression",
"cookie",
"dependency-injection",
"error-handle",
"file",
"grpc",
Expand Down
7 changes: 7 additions & 0 deletions examples/dependency-injection/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dependency-injection"
version = "0.1.0"
edition = "2021"

[dependencies]
xitca-web = "0.2.2"
48 changes: 48 additions & 0 deletions examples/dependency-injection/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! example for using trait object as application state.
use std::sync::Arc;

use xitca_web::{
handler::{handler_service, state::StateRef},
route::get,
App,
};

fn main() -> std::io::Result<()> {
App::new()
// use trait object as application state
.with_state(object_state())
// extract trait object in handler
.at("/", get(handler_service(handler)))
.serve()
.bind("127.0.0.1:8080")?
.run()
.wait()
}

// a simple trait for injecting dependent type and logic to application.
trait DI {
fn name(&self) -> &'static str;
}

// thread safe trait object state constructor.
fn object_state() -> Arc<dyn DI + Send + Sync> {
// a dummy type implementing DI trait.
struct Foo;

impl DI for Foo {
fn name(&self) -> &'static str {
"foo"
}
}

// only this function knows the exact type implementing DI trait.
// everywhere else in the application it's only known as dyn DI trait object.
Arc::new(Foo)
}

// StateRef extractor is able extract &dyn DI from application state.
async fn handler(StateRef(obj): StateRef<'_, dyn DI + Send + Sync>) -> String {
// get request to "/" path should return "hello foo" string response.
format!("hello {}", obj.name())
}
4 changes: 2 additions & 2 deletions test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ io-uring = ["xitca-http/io-uring", "xitca-server/io-uring"]

[dependencies]
xitca-client = { version = "0.1", features = ["http2", "http3", "websocket", "dangerous"] }
xitca-http = { version = "0.2", features = ["http2", "http3"] }
xitca-http = { version = "0.2.1", features = ["http2", "http3"] }
xitca-codegen = "0.1"
xitca-io = "0.1"
xitca-server = { version = "0.1", features = ["http3"] }
xitca-service = "0.1"
xitca-unsafe-collection = "0.1"
xitca-web = "0.2"
xitca-web = "0.2.2"

http-ws = { version = "0.2", features = ["stream"] }

Expand Down
4 changes: 3 additions & 1 deletion web/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# unreleased 0.2.2
# unreleased

# 0.2.2
## Add
- `StateRef` can used for extracting `?Sized` type from application state.

Expand Down

0 comments on commit ecc2fdf

Please sign in to comment.