-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed a bug that caused the definition of traits with immutable references as receivers to fail.
- Loading branch information
not-elm
committed
Feb 25, 2024
1 parent
6809078
commit f598bb4
Showing
7 changed files
with
94 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,50 @@ | ||
use std::path::Path; | ||
use async_trait::async_trait; | ||
|
||
use auto_delegate::{delegate, Delegate}; | ||
|
||
pub struct EmailAddress(String); | ||
|
||
impl EmailAddress { | ||
#[allow(unused)] | ||
pub fn raw(&self) -> &str { | ||
self.0.as_str() | ||
} | ||
#[delegate] | ||
trait ReadFile { | ||
async fn read_file(&self, path: impl AsRef<Path>) -> String; | ||
} | ||
|
||
|
||
impl Default for EmailAddress { | ||
fn default() -> Self { | ||
Self(String::from("rust@gmail.com")) | ||
} | ||
} | ||
|
||
|
||
#[async_trait] | ||
#[delegate] | ||
pub trait EmailReadable { | ||
async fn read_email<'a>(&'a self) -> &'a EmailAddress; | ||
#[async_trait] | ||
trait ReadFileBufLen { | ||
async fn read_file_buf_len(&self, path: impl AsRef<Path> + Send) -> usize; | ||
} | ||
|
||
|
||
#[derive(Default)] | ||
pub struct EmailReader { | ||
email: EmailAddress, | ||
struct Io; | ||
|
||
impl ReadFile for Io{ | ||
async fn read_file(&self, path: impl AsRef<Path>) -> String { | ||
tokio::fs::read_to_string(path).await.unwrap() | ||
} | ||
} | ||
|
||
|
||
#[async_trait] | ||
impl EmailReadable for EmailReader { | ||
async fn read_email<'a>(&'a self) -> &'a EmailAddress { | ||
&self.email | ||
impl ReadFileBufLen for Io { | ||
async fn read_file_buf_len(&self, path: impl AsRef<Path> + Send) -> usize { | ||
tokio::fs::read_to_string(path).await.unwrap().len() | ||
} | ||
} | ||
|
||
|
||
#[derive(Default, Delegate)] | ||
struct Parent { | ||
#[to(EmailReadable)] | ||
child: EmailReader, | ||
#[to(ReadFile, ReadFileBufLen)] | ||
child: Io, | ||
} | ||
|
||
|
||
#[tokio::main] | ||
async fn main() { | ||
const PATH: &str = "examples/structs/async_trait.txt"; | ||
let parent = Parent::default(); | ||
|
||
assert_eq!(parent.read_email().await.raw(), "rust@gmail.com"); | ||
|
||
assert_eq!(parent.read_file(PATH).await, "HELLO"); | ||
assert_eq!(parent.read_file_buf_len(PATH).await, 5); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
HELLO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use async_trait::async_trait; | ||
use auto_delegate_impl::{delegate, Delegate}; | ||
|
||
#[delegate] | ||
#[async_trait] | ||
trait AsyncTrait { | ||
async fn parse_usize(&mut self, num_str: &str) -> usize; | ||
|
||
async fn cache(&self) -> usize; | ||
} | ||
|
||
#[derive(Default)] | ||
struct Child{ | ||
cache: usize | ||
} | ||
|
||
#[async_trait] | ||
impl AsyncTrait for Child{ | ||
async fn parse_usize(&mut self, num_str: &str) -> usize { | ||
let num: usize = num_str.parse().unwrap(); | ||
self.cache = num; | ||
num | ||
} | ||
|
||
async fn cache(&self) -> usize { | ||
self.cache | ||
} | ||
} | ||
|
||
#[derive(Default, Delegate)] | ||
struct Parent { | ||
#[to(AsyncTrait, RegularAsync)] | ||
child: Child, | ||
} | ||
|
||
|
||
#[tokio::main] | ||
async fn main() { | ||
let mut parent = Parent::default(); | ||
|
||
assert_eq!(parent.parse_usize("2").await, 2); | ||
assert_eq!(parent.cache().await, 2); | ||
} |