update Rust

This commit is contained in:
xkeyC 2024-02-07 20:38:16 +08:00
parent 5fa62351f2
commit 95b4b8b947
7 changed files with 29 additions and 53 deletions

View File

@ -300,9 +300,9 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
// Codec=Dco (DartCObject based), see doc to use other codecs // Codec=Dco (DartCObject based), see doc to use other codecs
switch (raw[0]) { switch (raw[0]) {
case 0: case 0:
return const MyDownloaderStatus_NoStart(); return MyDownloaderStatus_NoStart();
case 1: case 1:
return const MyDownloaderStatus_Running(); return MyDownloaderStatus_Running();
case 2: case 2:
return MyDownloaderStatus_Pending( return MyDownloaderStatus_Pending(
dco_decode_my_network_item_pending_type(raw[1]), dco_decode_my_network_item_pending_type(raw[1]),
@ -312,7 +312,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
dco_decode_String(raw[1]), dco_decode_String(raw[1]),
); );
case 4: case 4:
return const MyDownloaderStatus_Finished(); return MyDownloaderStatus_Finished();
default: default:
throw Exception("unreachable"); throw Exception("unreachable");
} }
@ -501,9 +501,9 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
var tag_ = sse_decode_i_32(deserializer); var tag_ = sse_decode_i_32(deserializer);
switch (tag_) { switch (tag_) {
case 0: case 0:
return const MyDownloaderStatus_NoStart(); return MyDownloaderStatus_NoStart();
case 1: case 1:
return const MyDownloaderStatus_Running(); return MyDownloaderStatus_Running();
case 2: case 2:
var var_field0 = sse_decode_my_network_item_pending_type(deserializer); var var_field0 = sse_decode_my_network_item_pending_type(deserializer);
return MyDownloaderStatus_Pending(var_field0); return MyDownloaderStatus_Pending(var_field0);
@ -511,7 +511,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
var var_field0 = sse_decode_String(deserializer); var var_field0 = sse_decode_String(deserializer);
return MyDownloaderStatus_Error(var_field0); return MyDownloaderStatus_Error(var_field0);
case 4: case 4:
return const MyDownloaderStatus_Finished(); return MyDownloaderStatus_Finished();
default: default:
throw UnimplementedError(''); throw UnimplementedError('');
} }

View File

@ -3,13 +3,17 @@ name = "rust"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[profile.release]
opt-level = "s"
strip = "debuginfo"
[lib] [lib]
crate-type = ["cdylib", "staticlib"] crate-type = ["cdylib", "staticlib"]
[dependencies] [dependencies]
flutter_rust_bridge = "=2.0.0-dev.23" flutter_rust_bridge = "=2.0.0-dev.23"
http-downloader = { version = "0.3.2", features = ["status-tracker", "speed-tracker", "breakpoint-resume", "bson-file-archiver"] } http-downloader = { version = "0.3.2", features = ["status-tracker", "speed-tracker", "breakpoint-resume", "bson-file-archiver"] }
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }
url = "2.5.0" url = "2.5.0"
uuid = { version = "1.7.0", features = ["v4", "fast-rng", "macro-diagnostics"] } uuid = { version = "1.7.0", features = ["v4", "fast-rng", "macro-diagnostics"] }
async-std = "1.12.0" async-std = "1.12.0"
@ -17,3 +21,4 @@ hyper = { version = "0.14.28"}
once_cell = "1.19.0" once_cell = "1.19.0"
reqwest = { version = "0.11", features = ["rustls-tls-native-roots", "cookies", "gzip", "brotli", "deflate", "multipart", "trust-dns", "json","stream"] } reqwest = { version = "0.11", features = ["rustls-tls-native-roots", "cookies", "gzip", "brotli", "deflate", "multipart", "trust-dns", "json","stream"] }
hickory-resolver = {version = "0.24.0", features = [ "dns-over-https-rustls","dns-over-rustls","native-certs"]} hickory-resolver = {version = "0.24.0", features = [ "dns-over-https-rustls","dns-over-rustls","native-certs"]}
anyhow = "1.0"

View File

@ -2,8 +2,8 @@ use std::sync::Arc;
use crate::downloader::{do_cancel_download, do_start_download, DownloadCallbackData}; use crate::downloader::{do_cancel_download, do_start_download, DownloadCallbackData};
use crate::frb_generated::StreamSink; use crate::frb_generated::StreamSink;
pub fn start_download(url: String, save_path: String, file_name: String, connection_count: u8, sink: StreamSink<DownloadCallbackData>) { pub async fn start_download(url: String, save_path: String, file_name: String, connection_count: u8, sink: StreamSink<DownloadCallbackData>) {
let _ = do_start_download(url, save_path, file_name, connection_count, Arc::new(sink)); do_start_download(url, save_path, file_name, connection_count, Arc::new(sink)).await.unwrap();
} }
pub async fn cancel_download(id: String) { pub async fn cancel_download(id: String) {

View File

@ -1,5 +1,4 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error;
use std::num::{NonZeroU8, NonZeroUsize}; use std::num::{NonZeroU8, NonZeroUsize};
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
@ -63,8 +62,7 @@ pub async fn do_cancel_download(id: &str) {
d.unwrap().cancel().await d.unwrap().cancel().await
} }
#[tokio::main] pub async fn do_start_download(url: String, save_path: String, file_name: String, connection_count: u8, sink: Arc<StreamSink<DownloadCallbackData>>) -> anyhow::Result<()> {
pub async fn do_start_download(url: String, save_path: String, file_name: String, connection_count: u8, sink: Arc<StreamSink<DownloadCallbackData>>) -> Result<(), Box<dyn Error>> {
let save_dir = PathBuf::from(save_path); let save_dir = PathBuf::from(save_path);
let test_url = Url::parse(&*url)?; let test_url = Url::parse(&*url)?;

View File

@ -82,44 +82,17 @@ fn wire_start_download_impl(
rust_vec_len_: i32, rust_vec_len_: i32,
data_len_: i32, data_len_: i32,
) { ) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::<flutter_rust_bridge::for_generated::SseCodec, _, _>( FLUTTER_RUST_BRIDGE_HANDLER.wrap_async::<flutter_rust_bridge::for_generated::SseCodec,_,_,_>(flutter_rust_bridge::for_generated::TaskInfo{ debug_name: "start_download", port: Some(port_), mode: flutter_rust_bridge::for_generated::FfiCallMode::Stream }, move || {
flutter_rust_bridge::for_generated::TaskInfo { let message = unsafe { flutter_rust_bridge::for_generated::Dart2RustMessageSse::from_wire(ptr_, rust_vec_len_, data_len_) };
debug_name: "start_download", let mut deserializer = flutter_rust_bridge::for_generated::SseDeserializer::new(message);
port: Some(port_),
mode: flutter_rust_bridge::for_generated::FfiCallMode::Stream,
},
move || {
let message = unsafe {
flutter_rust_bridge::for_generated::Dart2RustMessageSse::from_wire(
ptr_,
rust_vec_len_,
data_len_,
)
};
let mut deserializer =
flutter_rust_bridge::for_generated::SseDeserializer::new(message);
let api_url = <String>::sse_decode(&mut deserializer); let api_url = <String>::sse_decode(&mut deserializer);
let api_save_path = <String>::sse_decode(&mut deserializer); let api_save_path = <String>::sse_decode(&mut deserializer);
let api_file_name = <String>::sse_decode(&mut deserializer); let api_file_name = <String>::sse_decode(&mut deserializer);
let api_connection_count = <u8>::sse_decode(&mut deserializer); let api_connection_count = <u8>::sse_decode(&mut deserializer);deserializer.end(); move |context| async move {
deserializer.end(); transform_result_sse((move || async move {
move |context| { Result::<_,()>::Ok(crate::api::downloader_api::start_download(api_url, api_save_path, api_file_name, api_connection_count, StreamSink::new(context.rust2dart_context().stream_sink::<_,crate::downloader::DownloadCallbackData>())).await)
transform_result_sse((move || { })().await)
Result::<_, ()>::Ok(crate::api::downloader_api::start_download( } })
api_url,
api_save_path,
api_file_name,
api_connection_count,
StreamSink::new(
context
.rust2dart_context()
.stream_sink::<_, crate::downloader::DownloadCallbackData>(),
),
))
})())
}
},
)
} }
fn wire_fetch_impl( fn wire_fetch_impl(
port_: flutter_rust_bridge::for_generated::MessagePort, port_: flutter_rust_bridge::for_generated::MessagePort,

View File

@ -43,12 +43,12 @@ impl Iterator for SocketAddrs {
} }
} }
fn new_resolver() -> io::Result<TokioAsyncResolver> { fn new_resolver() -> anyhow::Result<TokioAsyncResolver> {
let ali_ips: &[IpAddr] = &[ let ali_ips: &[IpAddr] = &[
IpAddr::V4(Ipv4Addr::new(223, 5, 5, 5)), IpAddr::V4(Ipv4Addr::new(223, 5, 5, 5)),
IpAddr::V4(Ipv4Addr::new(223, 6, 6, 6)), IpAddr::V4(Ipv4Addr::new(223, 6, 6, 6)),
IpAddr::V6("2400:3200::1".parse::<Ipv6Addr>().unwrap()), IpAddr::V6("2400:3200::1".parse::<Ipv6Addr>()?),
IpAddr::V6("2400:3200:baba::1".parse::<Ipv6Addr>().unwrap()), IpAddr::V6("2400:3200:baba::1".parse::<Ipv6Addr>()?),
]; ];
let group = let group =

View File

@ -48,7 +48,7 @@ pub async fn fetch(
url: String, url: String,
headers: Option<HashMap<String, String>>, headers: Option<HashMap<String, String>>,
input_data: Option<Vec<u8>>, input_data: Option<Vec<u8>>,
) -> reqwest::Result<RustHttpResponse> { ) -> anyhow::Result<RustHttpResponse> {
let mut req = _mix_header(HTTP_CLIENT.request(method, url), headers); let mut req = _mix_header(HTTP_CLIENT.request(method, url), headers);
if input_data.is_some() { if input_data.is_some() {
req = req.body(input_data.unwrap()); req = req.body(input_data.unwrap());