From 6b13c69293a1a7bc4e97f2318491df893d0f5567 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sun, 6 May 2018 23:34:07 -0600 Subject: [PATCH] working on tcp monad --- Chapter07/monad_pattern.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Chapter07/monad_pattern.rs b/Chapter07/monad_pattern.rs index f5910a7..e721986 100644 --- a/Chapter07/monad_pattern.rs +++ b/Chapter07/monad_pattern.rs @@ -1,4 +1,31 @@ use std::fmt::{Debug}; +use std::io::prelude::*; +use std::net::TcpListener; +use std::net::TcpStream; +use std::thread; + +struct ServerMonad { + state: St, + handle: Box bool> +} +impl ServerMonad { + fn _return(&self, st: St) -> ServerMonad { + ServerMonad { + state: st, + handle: Box::new(|st: &mut St, s| false) + } + } + fn listen(&self, address: &str) { + let listener = TcpListener::bind(address).unwrap(); + + for stream in listener.incoming() { + let mut st = self.state.clone(); + let mut buffer = String::new(); + stream.unwrap().read_to_string(&mut buffer); + (self.handle)(&mut st,buffer); + } + } +} struct LogMonad(T); impl LogMonad {