exercises

Log | Files | Refs | README

commit 581364cadb9ca1be794fe901e659ed22f2a4f718
parent 05e9362363c44ef39c34b36046f2b3c4de08d72c
Author: ling0x <ling0x@users.noreply.github.com>
Date:   Fri, 10 Jul 2026 11:52:00 +0100

async

Diffstat:
Aasync_programming_in_rust/bitmask/Cargo.lock | 7+++++++
Aasync_programming_in_rust/bitmask/Cargo.toml | 6++++++
Aasync_programming_in_rust/bitmask/src/main.rs | 22++++++++++++++++++++++
Masync_programming_in_rust/event_queue/src/ffi.rs | 34++++++++++++++++++++++++++++++++++
Masync_programming_in_rust/event_queue/src/poll.rs | 12++++++++++--
Adesign_patterns_in_rust/behavioral_patterns/src/chain_of_responsibility_pattern.rs | 1+
Mdesign_patterns_in_rust/behavioral_patterns/src/lib.rs | 1+
7 files changed, 81 insertions(+), 2 deletions(-)

diff --git a/async_programming_in_rust/bitmask/Cargo.lock b/async_programming_in_rust/bitmask/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bitmask" +version = "0.1.0" diff --git a/async_programming_in_rust/bitmask/Cargo.toml b/async_programming_in_rust/bitmask/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "bitmask" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/async_programming_in_rust/bitmask/src/main.rs b/async_programming_in_rust/bitmask/src/main.rs @@ -0,0 +1,22 @@ +fn main() { + let bitflag_a = 1 << 31; + let bitflag_b = 0x1; + + let bitmask = bitflag_a | bitflag_b; + println!("{bitflag_a:032b}"); + println!("{bitflag_b:032b}"); + println!("{bitmask:032b}"); + check(bitmask); +} + +fn check(bitmask: i32) { + const EPOLLIN: i32 = 0x1; + const EPOLLET: i32 = 1 << 31; + const EPOLLONESHOT: i32 = 0x40000000; + + let read = bitmask & EPOLLIN != 0; + let et = bitmask & EPOLLET != 0; + let oneshot = bitmask & EPOLLONESHOT != 0; + + println!("read_event? {read}, edge_trggered: {et}, oneshot?: {oneshot}"); +} diff --git a/async_programming_in_rust/event_queue/src/ffi.rs b/async_programming_in_rust/event_queue/src/ffi.rs @@ -1,2 +1,36 @@ //! This contain the code related to the syscalls we need to communicate //! with the host operating system + +// This structure is used to communicate with the operating system in +// epoll_ctl, and the operating system uses the same structure to communicate +// with us in epoll_wait +#[derive(Debug)] +#[repr(C, packed)] +pub struct Event { + // Events are defined as a u32, but it’s more than just a number. + // This field is what we call a bitmask. + pub(crate) events: u32, + // Token to identify event: + // A union is much like an enum, but in contrast to Rust’s enums, + // it doesn’t carry any information on what type it is, so it’s up + // to us to make sure we know what type of data it holds. + pub(crate) epoll_data: usize, +} + +impl Event { + pub fn token(&self) -> usize { + self.epoll_data + } +} + +pub const EPOLL_CTL_ADD: i32 = 1; +pub const EPOLLIN: i32 = 0x1; +pub const EPOLLET: i32 = 1 << 31; + +#[link(name = "c")] +unsafe extern "C" { + pub fn epoll_create(size: i32) -> i32; + pub fn close(fd: i32) -> i32; + pub fn epoll_ctl(epfd: i32, op: i32, fd: i32, event: *mut Event) -> i32; + pub fn epoll_wait(epfd: i32, events: *mut Event, maxevents: i32, timeout: i32) -> i32; +} diff --git a/async_programming_in_rust/event_queue/src/poll.rs b/async_programming_in_rust/event_queue/src/poll.rs @@ -2,6 +2,7 @@ //! There are two main abstractions over epoll. One is a structure called //! `Poll` and the other is called `Registry` +use crate::ffi; /// Its convenient to use `io::Result` type since most errors will stem from one /// of our calls into the operating system, and an operating system error can /// be mapped to an `io::Error` type @@ -10,7 +11,7 @@ use std::{ net::TcpStream, }; -type Events = Vec<ffi::Events>; +type Events = Vec<ffi::Event>; /// Poll is a struct that represents the event queue itself. pub struct Poll { @@ -20,7 +21,14 @@ pub struct Poll { impl Poll { /// Creates a new event queue pub fn new() -> Result<Self> { - todo!() + let res = unsafe { ffi::epoll_create(1) }; + if res < 0 { + return Err(io::Error::last_os_error()); + } + + Ok(Self { + registry: Registry { raw_fd: res }, + }) } /// Returns a reference to the registry that can be used to register diff --git a/design_patterns_in_rust/behavioral_patterns/src/chain_of_responsibility_pattern.rs b/design_patterns_in_rust/behavioral_patterns/src/chain_of_responsibility_pattern.rs @@ -0,0 +1 @@ + diff --git a/design_patterns_in_rust/behavioral_patterns/src/lib.rs b/design_patterns_in_rust/behavioral_patterns/src/lib.rs @@ -1,3 +1,4 @@ mod calculator; +mod chain_of_responsibility_pattern; mod command_pattern; mod expression;