Skip to content

Instantly share code, notes, and snippets.

use std::mem;
use std::sync::Arc;
use futures::task::{Context, Poll};
use tokio::sync::{Mutex, OwnedMutexGuard};
use tower_service::Service;
use tokio_util::sync::ReusableBoxFuture;
pub struct CloneableService<S> {
inner: Arc<Mutex<S>>,
mutex_fut: ReusableBoxFuture<OwnedMutexGuard<S>>,
fn transfer(&mut self, value_in: u8)
-> Result<u8, nb::Error<T::Error>>
{
// We always have to do a complete transfer - writing out and reading
// in. If we don't do the read, then the read buffer overflows and
// we can't use it when we need it.
self.spi.send(value_in)?;
Ok(self.spi.read()?)
}
@hgomersall
hgomersall / tokio_semaphore_test.rs
Created March 29, 2022 11:48
A quick a dirty test on how long a tokio semaphore takes to add and acquire permits (as a pair)
use tokio::sync::Semaphore;
use std::sync::Arc;
use std::time::Instant;
const LOOPS: usize = 1000000;
async fn twiddle(semaphore1: Arc<Semaphore>, semaphore2: Arc<Semaphore>) {
for _ in 0..LOOPS {
semaphore1.add_permits(1);
@hgomersall
hgomersall / monitored_writer.rs
Last active May 29, 2025 17:17
Example of how I'm showing what I put on the wire
#[derive(Debug)]
struct MonitoredWriter<W: AsyncWriteExt + Unpin> {
stream: W,
id_string: String,
held_buffer: Vec<u8>,
write_flag: AtomicBool,
}
impl<W: AsyncWriteExt + Unpin> MonitoredWriter<W> {
fn new(stream: W, id_string: String) -> MonitoredWriter<W> {
@hgomersall
hgomersall / stream.rs
Created May 29, 2025 16:51
Modifications to stream.rs in mio
use std::fmt;
use std::io::{self, IoSlice, IoSliceMut, Read, Write};
use std::net::{self, Shutdown, SocketAddr};
#[cfg(any(unix, target_os = "wasi"))]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
// TODO: once <https://github.com/rust-lang/rust/issues/126198> is fixed this
// can use `std::os::fd` and be merged with the above.
#[cfg(target_os = "hermit")]
use std::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]