8 个版本

0.0.11 2024 年 7 月 24 日
0.0.10 2023 年 11 月 6 日
0.0.9 2023 年 7 月 20 日
0.0.8 2023 年 6 月 24 日

开发工具 中排名 289

Download history 7/week @ 2024-04-27 94/week @ 2024-07-20 23/week @ 2024-07-27

每月下载量 117

MIT 许可证 MIT

44KB
1K SLoC


Rust 框架 🐦 Wildbird


介绍 👋

欢迎使用 Wildbird 🐦,专为简化 Rust 开发而设计。它配备了创建和管理服务 🧩 以及具有依赖注入 📌 功能的全局变量的工具。


目录


服务

一步创建服务实例(单例)

use wildbird::prelude::*;

// Convert struct to Service + impl construct()

#[service(construct = "init")]
struct HelloService {
    component_name: String,
}

impl HelloService {
    fn init() -> HelloService {
        HelloService {
            component_name: "Hello penguins 🐧".to_string(),
        }
    }

    fn say_hello(&self) {
        println!("Hello! 👋")
    }
}

fn main() {
    HelloService.say_hello();
}
  • 异步初始化
use wildbird::derive::*;

#[service(construct = "async init")]
struct AsyncService {}

impl AsyncService {
    async fn init() -> AsyncService {
        AsyncService {}
    }

    fn greeting(&self) {
        println!("Hello 🗼")
    }
}

fn main() {
    AsyncService.greeting();
}
  • 异步初始化函数
use wildbird::derive::*;

// Convert struct to Service
#[service]
struct HelloService {
    component_name: String,
}

// Impl Service trait construct() 
#[service(construct)]
async fn hello_init() -> HelloService {
    HelloService {
        component_name: "Hello 🚀".to_string(),
    }
}


全局变量

创建全局变量

use wildbird::derive::*;

#[var]
pub fn my_name() -> String {
    String::from("Hawk 🦅")
}

fn main() {
    println!("Hello from 🇵🇱, {}", &*MY_NAME);
}
  • 自定义名称
use wildbird::derive::*;

#[var(name = "HOME")]
fn custom_name() -> String {
    std::env::var("HOME").expect("env:HOME not found")
}

fn main() {
    println!("Home: {}", &*HOME);
}
  • 异步初始化
use std::time::Duration;
use wildbird::derive::*;
use std::thread::sleep;

#[var(name = "USERS")]
async fn http_fetch_users() -> String {
    sleep(Duration::from_millis(200));
    String::from("⏱️")
}
  • 回调初始化
use std::time::Duration;
use wildbird::derive::*;
use std::thread::sleep;

#[var(name = "PORT")]
async fn init_http_service(callback: wildbird::Callback<String>) {
    sleep(Duration::from_millis(200));
    println!("Server started");
    callback.call("8080".to_string());
}


依赖注入

注入支持目前仅限于 #[service)] 初始化方法。

例如

use wildbird::prelude::*;

#[service]
struct B {
    name: String,
}
#[service(construct)]
async fn b_init() -> B {
    B {
        name: "Baby 🐤".to_string(),
    }
}

#[service]
struct A {
    name: String,
    b_service: Arc<B>
}
#[service(construct)]
async fn a_init(b: Arc<B>) -> A {
    A {
        name: "Automobile 🚗".to_string(),
        b_service: b
    }
}


入门

添加依赖项

Cargo.toml

[dependencies]
wildbird = "^0.0.11"
功能标志

可选功能

  • tokio - 用于支持 tokio 异步环境
[dependencies]
tokio = "1.28"
wildbird = {version = "^0.0.11", features = ["tokio"]}

项目状态

项目处于早期开发阶段。每个版本都经过了预先测试,但随着项目的发展,API 变更很可能在未来发生。



创建者


本项目采用 MIT 许可证。


回到顶部 ⬆️



依赖项

~2–10MB
~111K SLoC