Skip to content

Commit e5851a2

Browse files
authored
Replace trait with enum in Rust (#575)
Async methods on traits require [_Pin<Box<dyn Future>>]_ return types, which pollute the public API. Additionally, C# interfaces and Rust traits are not equivalent abstractions, and traits offer no obvious advantage here. Since the set of implementors is closed and internal, an enum with private variants provides the same polymorphism with a cleaner, more idiomatic Rust API.
1 parent 8e57f69 commit e5851a2

18 files changed

Lines changed: 325 additions & 431 deletions

File tree

samples/rust/audio-transcription-example/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3535
if !model.is_cached().await? {
3636
println!("Downloading model...");
3737
model
38-
.download(Some(Box::new(|progress: &str| {
38+
.download(Some(|progress: &str| {
3939
print!("\r {progress}%");
4040
io::stdout().flush().ok();
41-
})))
41+
}))
4242
.await?;
4343
println!();
4444
}

samples/rust/foundry-local-webserver/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3434
if !model.is_cached().await? {
3535
print!("Downloading model {model_alias}...");
3636
model
37-
.download(Some(Box::new(move |progress: &str| {
37+
.download(Some(move |progress: &str| {
3838
print!("\rDownloading model... {progress}%");
3939
io::stdout().flush().ok();
40-
})))
40+
}))
4141
.await?;
4242
println!();
4343
}

samples/rust/native-chat-completions/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3232
if !model.is_cached().await? {
3333
println!("Downloading model...");
3434
model
35-
.download(Some(Box::new(|progress: &str| {
35+
.download(Some(|progress: &str| {
3636
print!("\r {progress}%");
3737
io::stdout().flush().ok();
38-
})))
38+
}))
3939
.await?;
4040
println!();
4141
}

samples/rust/tool-calling-foundry-local/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
6666
if !model.is_cached().await? {
6767
println!("Downloading model...");
6868
model
69-
.download(Some(Box::new(|progress: &str| {
69+
.download(Some(|progress: &str| {
7070
print!("\r {progress}%");
7171
io::stdout().flush().ok();
72-
})))
72+
}))
7373
.await?;
7474
println!();
7575
}

samples/rust/tutorial-chat-assistant/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ async fn main() -> anyhow::Result<()> {
2121
if !model.is_cached().await? {
2222
println!("Downloading model...");
2323
model
24-
.download(Some(Box::new(|progress: &str| {
24+
.download(Some(|progress: &str| {
2525
print!("\r {progress}");
2626
io::stdout().flush().ok();
27-
})))
27+
}))
2828
.await?;
2929
println!();
3030
}

samples/rust/tutorial-document-summarizer/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ async fn main() -> anyhow::Result<()> {
9696
if !model.is_cached().await? {
9797
println!("Downloading model...");
9898
model
99-
.download(Some(Box::new(|progress: &str| {
99+
.download(Some(|progress: &str| {
100100
print!("\r {progress}");
101101
io::stdout().flush().ok();
102-
})))
102+
}))
103103
.await?;
104104
println!();
105105
}

samples/rust/tutorial-tool-calling/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ async fn main() -> anyhow::Result<()> {
199199
if !model.is_cached().await? {
200200
println!("Downloading model...");
201201
model
202-
.download(Some(Box::new(|progress: &str| {
202+
.download(Some(|progress: &str| {
203203
print!("\r {progress}");
204204
io::stdout().flush().ok();
205-
})))
205+
}))
206206
.await?;
207207
println!();
208208
}

samples/rust/tutorial-voice-to-text/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ async fn main() -> anyhow::Result<()> {
2828
if !speech_model.is_cached().await? {
2929
println!("Downloading speech model...");
3030
speech_model
31-
.download(Some(Box::new(|progress: &str| {
31+
.download(Some(|progress: &str| {
3232
print!("\r {progress}");
3333
io::stdout().flush().ok();
34-
})))
34+
}))
3535
.await?;
3636
println!();
3737
}
@@ -60,10 +60,10 @@ async fn main() -> anyhow::Result<()> {
6060
if !chat_model.is_cached().await? {
6161
println!("Downloading chat model...");
6262
chat_model
63-
.download(Some(Box::new(|progress: &str| {
63+
.download(Some(|progress: &str| {
6464
print!("\r {progress}");
6565
io::stdout().flush().ok();
66-
})))
66+
}))
6767
.await?;
6868
println!();
6969
}

sdk/rust/examples/chat_completion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ async fn main() -> Result<()> {
4040
if !model.is_cached().await? {
4141
println!("Downloading model '{}'…", model.alias());
4242
model
43-
.download(Some(Box::new(|progress: &str| {
43+
.download(Some(|progress: &str| {
4444
println!(" {progress}");
45-
})))
45+
}))
4646
.await?;
4747
}
4848

sdk/rust/examples/interactive_chat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4141
// Download if needed
4242
if !model.is_cached().await? {
4343
println!("Downloading '{alias}'…");
44-
model.download(Some(Box::new(|p: &str| print!("\r {p}%")))).await?;
44+
model.download(Some(|p: &str| print!("\r {p}%"))).await?;
4545
println!();
4646
}
4747

0 commit comments

Comments
 (0)