pub struct AdvancedDownloader { /* private fields */ }Expand description
High-performance downloader with parallel connections and resume support.
AdvancedDownloader is the recommended way to download large files. It provides:
- Parallel chunk downloads: Splits files into segments downloaded simultaneously
- Automatic resume: Detects existing partial files and resumes from last position
- Server compatibility: Falls back to single-stream if server doesn’t support ranges
- ISO optimization: Disables compression for binary files to prevent corruption
- Progress tracking: Real-time callbacks for UI integration
- Cancellation support: Stop downloads gracefully via atomic cancel token
§Example
use kget::{AdvancedDownloader, ProxyConfig, Optimizer};
let downloader = AdvancedDownloader::new(
"https://example.com/large-file.zip".to_string(),
"large-file.zip".to_string(),
false, // quiet_mode
ProxyConfig::default(),
Optimizer::new(),
);
downloader.download().expect("Download failed");§With Progress Tracking
use kget::{AdvancedDownloader, ProxyConfig, Optimizer};
use std::sync::Arc;
let mut dl = AdvancedDownloader::new(
"https://example.com/file.iso".to_string(),
"file.iso".to_string(),
true, // quiet mode (no stdout)
ProxyConfig::default(),
Optimizer::new(),
);
dl.set_progress_callback(Arc::new(|p| {
// p is 0.0 to 1.0
update_ui_progress(p);
}));
dl.set_status_callback(Arc::new(|msg| {
log::info!("{}", msg);
}));
dl.download().unwrap();
fn update_ui_progress(p: f32) {
// Update your UI here
}Implementations§
Source§impl AdvancedDownloader
impl AdvancedDownloader
Sourcepub fn new(
url: String,
output_path: String,
quiet_mode: bool,
proxy_config: ProxyConfig,
optimizer: Optimizer,
) -> Self
pub fn new( url: String, output_path: String, quiet_mode: bool, proxy_config: ProxyConfig, optimizer: Optimizer, ) -> Self
Create a new AdvancedDownloader instance.
§Arguments
url- URL to download fromoutput_path- Local path for the downloaded filequiet_mode- If true, suppress console outputproxy_config- Proxy settings (useProxyConfig::default()for direct connection)optimizer- Optimizer for connection settings
§Example
use kget::{AdvancedDownloader, ProxyConfig, Optimizer};
let dl = AdvancedDownloader::new(
"https://example.com/file.zip".to_string(),
"./downloads/file.zip".to_string(),
false,
ProxyConfig::default(),
Optimizer::new(),
);Sourcepub fn set_cancel_token(&mut self, token: Arc<AtomicBool>)
pub fn set_cancel_token(&mut self, token: Arc<AtomicBool>)
Set a custom cancellation token for graceful download interruption.
When the token is set to true, the download will stop at the next
checkpoint and return an error.
§Example
use kget::{AdvancedDownloader, ProxyConfig, Optimizer};
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
let cancel = Arc::new(AtomicBool::new(false));
let mut dl = AdvancedDownloader::new(/* ... */
);
dl.set_cancel_token(cancel.clone());
// In another thread:
// cancel.store(true, std::sync::atomic::Ordering::Relaxed);Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Check if the download has been cancelled.
Sourcepub fn set_progress_callback(
&mut self,
callback: impl Fn(f32) + Send + Sync + 'static,
)
pub fn set_progress_callback( &mut self, callback: impl Fn(f32) + Send + Sync + 'static, )
Set a callback for progress updates.
The callback receives a value from 0.0 (start) to 1.0 (complete).
§Example
dl.set_progress_callback(|progress| {
println!("Downloaded: {:.1}%", progress * 100.0);
});Sourcepub fn set_status_callback(
&mut self,
callback: impl Fn(String) + Send + Sync + 'static,
)
pub fn set_status_callback( &mut self, callback: impl Fn(String) + Send + Sync + 'static, )
Set a callback for status messages.
Receives human-readable status updates during the download.
§Example
dl.set_status_callback(|msg| {
log::info!("Download status: {}", msg);
});Sourcepub fn download(&self) -> Result<(), Box<dyn Error + Send + Sync>>
pub fn download(&self) -> Result<(), Box<dyn Error + Send + Sync>>
Start the download.
This method:
- Checks for existing partial file (resume support)
- Queries server for file size and range support
- Downloads using parallel connections if supported
- Falls back to single-stream if server doesn’t support ranges
§Returns
Returns Ok(()) on successful download, or an error if the download fails.
§Errors
- Network connection failures
- Existing file larger than remote (corrupted state)
- Cancellation via cancel token
- Disk I/O errors
Auto Trait Implementations§
impl Freeze for AdvancedDownloader
impl !RefUnwindSafe for AdvancedDownloader
impl Send for AdvancedDownloader
impl Sync for AdvancedDownloader
impl Unpin for AdvancedDownloader
impl UnsafeUnpin for AdvancedDownloader
impl !UnwindSafe for AdvancedDownloader
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more