Skip to content

Commit 7056c1f

Browse files
authored
Unseal the Sync- and AsyncInput traits (#1315)
1 parent 1a5d274 commit 7056c1f

File tree

4 files changed

+16
-25
lines changed

4 files changed

+16
-25
lines changed

rust/lib/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Minor
66

7+
- Unseal `SyncInput` and `AsyncInput` for custom file-like objects
78
- Support files larger than 4GB on 32-bits architectures
89

910
## 1.0.2

rust/lib/src/future.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ use ndarray::Array2;
2323
use ort::session::{NoSelectedOutputs, RunOptions};
2424
use ort::value::Tensor;
2525

26-
use crate::input::AsyncInputApi;
27-
use crate::Result;
26+
use crate::{AsyncInput, Result};
2827

2928
pub(crate) fn exec<T>(mut future: impl Future<Output = T>) -> T {
3029
let future = unsafe { Pin::new_unchecked(&mut future) };
@@ -37,7 +36,7 @@ pub(crate) fn exec<T>(mut future: impl Future<Output = T>) -> T {
3736
}
3837

3938
pub(crate) trait Env {
40-
type File: AsyncInputApi;
39+
type File: AsyncInput;
4140
async fn symlink_metadata(path: &Path) -> Result<Metadata>;
4241
async fn open(path: &Path) -> Result<Self::File>;
4342
async fn ort_session_run(

rust/lib/src/input.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,16 @@ use crate::{ContentType, Result};
2525
pub struct Features(pub(crate) Vec<i32>);
2626

2727
/// Synchronous abstraction over file content.
28-
pub trait SyncInput: SyncInputApi {}
29-
30-
/// Asynchronous abstraction over file content.
31-
pub trait AsyncInput: AsyncInputApi {}
32-
33-
pub trait SyncInputApi {
28+
pub trait SyncInput {
3429
/// Returns the size of the input.
3530
fn length(&self) -> Result<u64>;
3631

3732
/// Reads from the input at the given offset to fill the buffer.
3833
fn read_at(&mut self, buffer: &mut [u8], offset: u64) -> Result<()>;
3934
}
4035

41-
pub trait AsyncInputApi {
36+
/// Asynchronous abstraction over file content.
37+
pub trait AsyncInput {
4238
/// Returns the size of the input.
4339
fn length(&self) -> impl Future<Output = Result<u64>>;
4440

@@ -51,8 +47,7 @@ const _: () = const {
5147
assert!(std::mem::size_of::<usize>() <= std::mem::size_of::<u64>());
5248
};
5349

54-
impl SyncInput for &[u8] {}
55-
impl SyncInputApi for &[u8] {
50+
impl SyncInput for &[u8] {
5651
fn length(&self) -> Result<u64> {
5752
Ok(self.len() as u64)
5853
}
@@ -64,8 +59,7 @@ impl SyncInputApi for &[u8] {
6459
}
6560
}
6661

67-
impl SyncInput for std::fs::File {}
68-
impl SyncInputApi for std::fs::File {
62+
impl SyncInput for std::fs::File {
6963
fn length(&self) -> Result<u64> {
7064
Ok(self.metadata()?.len())
7165
}
@@ -76,18 +70,17 @@ impl SyncInputApi for std::fs::File {
7670
}
7771
}
7872

79-
impl<T: SyncInputApi> SyncInput for &mut T {}
80-
impl<T: SyncInputApi> SyncInputApi for &mut T {
73+
impl<T: SyncInput> SyncInput for &mut T {
8174
fn length(&self) -> Result<u64> {
82-
<T as SyncInputApi>::length(self)
75+
<T as SyncInput>::length(self)
8376
}
8477

8578
fn read_at(&mut self, buffer: &mut [u8], offset: u64) -> Result<()> {
86-
<T as SyncInputApi>::read_at(self, buffer, offset)
79+
<T as SyncInput>::read_at(self, buffer, offset)
8780
}
8881
}
8982

90-
impl<T: SyncInputApi> AsyncInputApi for T {
83+
impl<T: SyncInput> AsyncInput for T {
9184
fn length(&self) -> impl Future<Output = Result<u64>> {
9285
std::future::ready(self.length())
9386
}
@@ -97,8 +90,7 @@ impl<T: SyncInputApi> AsyncInputApi for T {
9790
}
9891
}
9992

100-
impl AsyncInput for tokio::fs::File {}
101-
impl AsyncInputApi for tokio::fs::File {
93+
impl AsyncInput for tokio::fs::File {
10294
async fn length(&self) -> Result<u64> {
10395
Ok(self.metadata().await?.len())
10496
}
@@ -134,7 +126,7 @@ impl FeaturesOrRuled {
134126
Self::extract(file).await
135127
}
136128

137-
pub(crate) async fn extract(file: impl AsyncInputApi) -> Result<Self> {
129+
pub(crate) async fn extract(file: impl AsyncInput) -> Result<Self> {
138130
let config = &crate::model::CONFIG;
139131
let file_len = file.length().await?;
140132
if file_len == 0 {
@@ -154,7 +146,7 @@ impl FeaturesOrRuled {
154146
}
155147

156148
async fn extract_features_async(
157-
config: &ModelConfig, mut file: impl AsyncInputApi, file_len: u64,
149+
config: &ModelConfig, mut file: impl AsyncInput, file_len: u64,
158150
) -> Result<(Vec<u8>, Vec<i32>)> {
159151
debug_assert!(config.beg_size < config.block_size);
160152
debug_assert!(config.end_size < config.block_size);

rust/lib/src/session.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use std::path::Path;
1717
use ndarray::Array2;
1818

1919
use crate::future::{exec, AsyncEnv, Env, SyncEnv};
20-
use crate::input::AsyncInputApi;
2120
use crate::{AsyncInput, Builder, Features, FeaturesOrRuled, FileType, Result, SyncInput};
2221

2322
/// A Magika session to identify files.
@@ -69,7 +68,7 @@ impl Session {
6968
self.identify_content::<AsyncEnv>(file).await
7069
}
7170

72-
async fn identify_content<E: Env>(&mut self, file: impl AsyncInputApi) -> Result<FileType> {
71+
async fn identify_content<E: Env>(&mut self, file: impl AsyncInput) -> Result<FileType> {
7372
match FeaturesOrRuled::extract(file).await? {
7473
FeaturesOrRuled::Ruled(content_type) => Ok(FileType::Ruled(content_type)),
7574
FeaturesOrRuled::Features(features) => self.identify_features::<E>(&features).await,

0 commit comments

Comments
 (0)