引入 Rust \\TODO 使用 rust 实现的下载器

This commit is contained in:
2023-11-04 22:55:48 +08:00
parent 0b2c1c8f92
commit 0786d30300
14 changed files with 654 additions and 1 deletions

63
rust/src/api.rs Normal file
View File

@ -0,0 +1,63 @@
// This is the entry point of your Rust library.
// When adding new code to your project, note that only items used
// here will be transformed to their Dart equivalents.
// A plain enum without any fields. This is similar to Dart- or C-style enums.
// flutter_rust_bridge is capable of generating code for enums with fields
// (@freezed classes in Dart and tagged unions in C).
pub enum Platform {
Unknown,
Android,
Ios,
Windows,
Unix,
MacIntel,
MacApple,
Wasm,
}
// A function definition in Rust. Similar to Dart, the return type must always be named
// and is never inferred.
pub fn platform() -> Platform {
// This is a macro, a special expression that expands into code. In Rust, all macros
// end with an exclamation mark and can be invoked with all kinds of brackets (parentheses,
// brackets and curly braces). However, certain conventions exist, for example the
// vector macro is almost always invoked as vec![..].
//
// The cfg!() macro returns a boolean value based on the current compiler configuration.
// When attached to expressions (#[cfg(..)] form), they show or hide the expression at compile time.
// Here, however, they evaluate to runtime values, which may or may not be optimized out
// by the compiler. A variety of configurations are demonstrated here which cover most of
// the modern oeprating systems. Try running the Flutter application on different machines
// and see if it matches your expected OS.
//
// Furthermore, in Rust, the last expression in a function is the return value and does
// not have the trailing semicolon. This entire if-else chain forms a single expression.
if cfg!(windows) {
Platform::Windows
} else if cfg!(target_os = "android") {
Platform::Android
} else if cfg!(target_os = "ios") {
Platform::Ios
} else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
Platform::MacApple
} else if cfg!(target_os = "macos") {
Platform::MacIntel
} else if cfg!(target_family = "wasm") {
Platform::Wasm
} else if cfg!(unix) {
Platform::Unix
} else {
Platform::Unknown
}
}
pub fn add(left: usize, right: usize) -> usize {
left + right
}
// The convention for Rust identifiers is the snake_case,
// and they are automatically converted to camelCase on the Dart side.
pub fn rust_release_mode() -> bool {
cfg!(not(debug_assertions))
}

View File

@ -0,0 +1,46 @@
use super::*;
// Section: wire functions
#[no_mangle]
pub extern "C" fn wire_platform(port_: i64) {
wire_platform_impl(port_)
}
#[no_mangle]
pub extern "C" fn wire_add(port_: i64, left: usize, right: usize) {
wire_add_impl(port_, left, right)
}
#[no_mangle]
pub extern "C" fn wire_rust_release_mode(port_: i64) {
wire_rust_release_mode_impl(port_)
}
// Section: allocate functions
// Section: related functions
// Section: impl Wire2Api
// Section: wire structs
// Section: impl NewWithNullPtr
pub trait NewWithNullPtr {
fn new_with_null_ptr() -> Self;
}
impl<T> NewWithNullPtr for *mut T {
fn new_with_null_ptr() -> Self {
std::ptr::null_mut()
}
}
// Section: sync execution mode utility
#[no_mangle]
pub extern "C" fn free_WireSyncReturn(ptr: support::WireSyncReturn) {
unsafe {
let _ = support::box_from_leak_ptr(ptr);
};
}

View File

@ -0,0 +1,43 @@
use super::*;
// Section: wire functions
#[no_mangle]
pub extern "C" fn wire_add(port_: i64,left: usize,right: usize) {
wire_add_impl(port_,left,right)
}
// Section: allocate functions
// Section: related functions
// Section: impl Wire2Api
// Section: wire structs
// Section: impl NewWithNullPtr
pub trait NewWithNullPtr {
fn new_with_null_ptr() -> Self;
}
impl<T> NewWithNullPtr for *mut T {
fn new_with_null_ptr() -> Self {
std::ptr::null_mut()
}
}
// Section: sync execution mode utility
#[no_mangle]
pub extern "C" fn free_WireSyncReturn(ptr: support::WireSyncReturn) {
unsafe { let _ = support::box_from_leak_ptr(ptr); };
}

View File

@ -0,0 +1,124 @@
#![allow(
non_camel_case_types,
unused,
clippy::redundant_closure,
clippy::useless_conversion,
clippy::unit_arg,
clippy::double_parens,
non_snake_case,
clippy::too_many_arguments
)]
// AUTO GENERATED FILE, DO NOT EDIT.
// Generated by `flutter_rust_bridge`@ 1.82.3.
use crate::api::*;
use core::panic::UnwindSafe;
use flutter_rust_bridge::rust2dart::IntoIntoDart;
use flutter_rust_bridge::*;
use std::ffi::c_void;
use std::sync::Arc;
// Section: imports
// Section: wire functions
fn wire_platform_impl(port_: MessagePort) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap::<_, _, _, Platform, _>(
WrapInfo {
debug_name: "platform",
port: Some(port_),
mode: FfiCallMode::Normal,
},
move || move |task_callback| Result::<_, ()>::Ok(platform()),
)
}
fn wire_add_impl(
port_: MessagePort,
left: impl Wire2Api<usize> + UnwindSafe,
right: impl Wire2Api<usize> + UnwindSafe,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap::<_, _, _, usize, _>(
WrapInfo {
debug_name: "add",
port: Some(port_),
mode: FfiCallMode::Normal,
},
move || {
let api_left = left.wire2api();
let api_right = right.wire2api();
move |task_callback| Result::<_, ()>::Ok(add(api_left, api_right))
},
)
}
fn wire_rust_release_mode_impl(port_: MessagePort) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap::<_, _, _, bool, _>(
WrapInfo {
debug_name: "rust_release_mode",
port: Some(port_),
mode: FfiCallMode::Normal,
},
move || move |task_callback| Result::<_, ()>::Ok(rust_release_mode()),
)
}
// Section: wrapper structs
// Section: static checks
// Section: allocate functions
// Section: related functions
// Section: impl Wire2Api
pub trait Wire2Api<T> {
fn wire2api(self) -> T;
}
impl<T, S> Wire2Api<Option<T>> for *mut S
where
*mut S: Wire2Api<T>,
{
fn wire2api(self) -> Option<T> {
(!self.is_null()).then(|| self.wire2api())
}
}
impl Wire2Api<usize> for usize {
fn wire2api(self) -> usize {
self
}
}
// Section: impl IntoDart
impl support::IntoDart for Platform {
fn into_dart(self) -> support::DartAbi {
match self {
Self::Unknown => 0,
Self::Android => 1,
Self::Ios => 2,
Self::Windows => 3,
Self::Unix => 4,
Self::MacIntel => 5,
Self::MacApple => 6,
Self::Wasm => 7,
}
.into_dart()
}
}
impl support::IntoDartExceptPrimitive for Platform {}
impl rust2dart::IntoIntoDart<Platform> for Platform {
fn into_into_dart(self) -> Self {
self
}
}
// Section: executor
support::lazy_static! {
pub static ref FLUTTER_RUST_BRIDGE_HANDLER: support::DefaultHandler = Default::default();
}
#[cfg(not(target_family = "wasm"))]
#[path = "bridge_generated.io.rs"]
mod io;
#[cfg(not(target_family = "wasm"))]
pub use io::*;

2
rust/src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
mod api;
mod bridge_generated; /* AUTO INJECTED BY flutter_rust_bridge. This line may not be accurate, and you can change it according to your needs. */