-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Task runner (mid-level API), part 1 #172
Conversation
Pull Request Test Coverage Report for Build 9085261933Details
💛 - Coveralls |
I don't understand why we need a Suggestion: Let's add this method to impl Scheduler {
pub fn run_task<'a, T: Task>(&'a mut self, task: T) -> TaskRunner<'a, T> {
let task_handle = self.enqueue_task(task);
TaskRunner { scheduler: self, task_handle }
}
}
pub struct TaskRunner<'a, T: Task> {
scheduler: &'a mut Scheduler,
task_handle: TaskHandle<T>,
}
impl<'a, T: Task> Flow for TaskRunner<'a, T> {
type Event = T::Output;
type Error = SchedulerError;
fn enqueue_input(&mut self, bytes: &[u8]) {
self.scheduler.enqueue_input(bytes);
}
fn progress(&mut self) -> Result<Self::Event, FlowInterrupt<Self::Error>> {
todo!()
}
} Then we can use it like this: async fn main() {
let stream = TcpStream::connect("127.0.0.1:12345").await.unwrap();
let mut stream = Stream::insecure(stream);
let client = ClientFlow::new(ClientFlowOptions::default());
let mut scheduler = Scheduler::new(client);
let capabilities = stream
.progress(scheduler.run_task(CapabilityTask::default()))
.await
.unwrap();
println!("capabilities: {capabilities:?}");
stream
.progress(scheduler.run_task(AuthenticateTask::plain("alice", "pa²²w0rd")))
.await
.unwrap();
stream
.progress(scheduler.run_task(LogoutTask::default()))
.await
.unwrap();
} If we want to run tasks in parallel, we could even provide something like this: let (foo, bar) = stream
.progress(scheduler.run_task_2(FooTask::default(), BarTask::default()))
.await
.unwrap(); And we could play a little bit with the API to make it more readable: let foo = stream
.progress(FooTask::default().run(scheduler)) // Swap order to reduce nesting
.await
.unwrap(); Pros:
What do you think? |
I initially tried sth similar, but the compiler was not happy at all about the lifetime introduction. Let me try again. |
Awesome, looks like it works well this time. I did not think about creating a new struct from the scheduler itself that returns self, somehow the compiler is happy. Thank you, I prefer this version more! |
(Regarding the CI, I wait for #175 to be merged to fix it) EDIT: done! |
I'm on vacation for a couple of days. I'll provide some feedback next week. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey Clement, some preliminary comments :-)
Hello! Thanks for progressing this PR. We were just discussing it and stumbled upon a design issue that is created by having let handle = scheduler.enqueue_task(AuthenticateTask::plain("alice", "pa²²w0rd", true));
let capabilities = stream
.progress(scheduler.run_task(CapabilityTask::new()))
.await // This will silently ignore the result of the enqueued AuthenticateTask
.unwrap()
.unwrap(); Having both methods on the same type might lead to accidental misusage. Maybe you were right about introducing a dedicated type called
However, these are long-term thoughts. If you are happy with the current state then let's keep it for now and improve the design later. I'm sure I'll change my mind about this a couple of times :p |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thanks! As soon as the remaining discussions are fixed we should quickly merge :-) I'm already accepting, thanks for the perseverence!
Indeed, I makes sense to separate them to avoid misuage. I initiated a new module Ready for another review! |
Thanks again! I added some more comments, but only minor issues, you can choose whether you want to fix them. If you are ready, we can merge it. |
Fixes pushed, waiting for the CI. If no more comment then it can be merged straight! I will open another PR with all other tasks. Thank you 🙂 |
Split version of #165, part 1.
Resolver
flow that aims to enqueue task AND get resolution straight.Resolver::run_task
returns a newFlow
calledTaskResolver
that can feedStream::progress
.Scheduler
andResolver
areSend
.