Skip to content

Commit

Permalink
chore(examples): use std lazy lock instead of once_cell
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Jul 25, 2024
1 parent d94cbaa commit 5bc88d7
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 17 deletions.
1 change: 0 additions & 1 deletion examples/htmlx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ serde_json.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }

handlebars = { version = "5", features = ["dir_source"] }
once_cell = "1"
5 changes: 2 additions & 3 deletions examples/htmlx/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// #![deny(warnings)]

use handlebars::{DirectorySourceOptions, Handlebars};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{
env,
net::SocketAddr,
path::PathBuf,
sync::{Arc, Mutex, PoisonError},
sync::{Arc, LazyLock, Mutex, PoisonError},
};
use tokio::net::TcpListener;
use viz::{
Expand All @@ -25,7 +24,7 @@ struct Todo {
pub completed: bool,
}

static TPLS: Lazy<Handlebars> = Lazy::new(|| {
static TPLS: LazyLock<Handlebars> = LazyLock::new(|| {
let dir = env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap();
let mut h = Handlebars::new();
h.register_templates_directory(dir.join("templates"), DirectorySourceOptions::default())
Expand Down
1 change: 0 additions & 1 deletion examples/static-routes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ viz.workspace = true

hyper.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
once_cell = "1"
8 changes: 5 additions & 3 deletions examples/static-routes/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

use hyper::server::conn::http1;
use hyper::service::service_fn;
use once_cell::sync::Lazy;
use std::{net::SocketAddr, sync::Arc};
use std::{
net::SocketAddr,
sync::{Arc, LazyLock},
};
use tokio::net::TcpListener;

use viz::{
Expand All @@ -13,7 +15,7 @@ use viz::{
};

/// Static Lazy Routes
static TREE: Lazy<Tree> = Lazy::new(|| Router::new().get("/", index).get("/me", me).into());
static TREE: LazyLock<Tree> = LazyLock::new(|| Router::new().get("/", index).get("/me", me).into());

async fn index(_: Request) -> Result<&'static str> {
Ok("Hello, World!")
Expand Down
1 change: 0 additions & 1 deletion examples/templates/minijinja/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ viz.workspace = true
serde.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
minijinja = { version = "2", features = ["loader"] }
once_cell = "1.19"
5 changes: 2 additions & 3 deletions examples/templates/minijinja/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#![deny(warnings)]
#![allow(clippy::unused_async)]

use std::{env, net::SocketAddr, path::PathBuf};
use std::{env, net::SocketAddr, path::PathBuf, sync::LazyLock};

use minijinja::{context, path_loader, Environment};
use once_cell::sync::Lazy;
use serde::Serialize;
use tokio::net::TcpListener;
use viz::{serve, Error, Request, Response, ResponseExt, Result, Router};

static TPLS: Lazy<Environment> = Lazy::new(|| {
static TPLS: LazyLock<Environment> = LazyLock::new(|| {
let dir = env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap();
let mut env = Environment::new();
env.set_loader(path_loader(dir.join("templates")));
Expand Down
1 change: 0 additions & 1 deletion examples/templates/tera/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ serde.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }

tera = "1.19"
once_cell = "1"
7 changes: 3 additions & 4 deletions examples/templates/tera/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#![deny(warnings)]

use std::net::SocketAddr;
use std::{net::SocketAddr, sync::LazyLock};

use once_cell::sync::Lazy;
use serde::Serialize;
use tera::{Context, Tera};
use tokio::net::TcpListener;
use viz::{serve, Error, Request, Response, ResponseExt, Result, Router};

static TPLS: Lazy<Tera> =
Lazy::new(|| Tera::new("examples/templates/tera/templates/**/*").unwrap());
static TPLS: LazyLock<Tera> =
LazyLock::new(|| Tera::new("examples/templates/tera/templates/**/*").unwrap());

#[derive(Serialize)]
struct User<'a> {
Expand Down

0 comments on commit 5bc88d7

Please sign in to comment.