1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![feature(type_alias_impl_trait)]

pub mod gym;

use self::gym::*;

use serde::{Deserialize, Serialize};
use tokio_serde::*;

pub struct BinCodec;

impl<T> Serializer<T> for BinCodec
where
    T: Serialize,
{
    type Error = std::io::Error;

    fn serialize(self: std::pin::Pin<&mut Self>, val: &T) -> Result<bytes::Bytes, Self::Error> {
        bincode::serialize(val)
            .map(|x| x.into())
            .map_err(|x| match *x {
                bincode::ErrorKind::Io(io) => io,
                _ => std::io::Error::new(std::io::ErrorKind::Other, "non-io error."),
            })
    }
}

impl<T> Deserializer<T> for BinCodec
where
    T: for<'de> Deserialize<'de>,
{
    type Error = std::io::Error;

    fn deserialize(
        self: std::pin::Pin<&mut Self>,
        bytes: &bytes::BytesMut,
    ) -> Result<T, Self::Error> {
        bincode::deserialize(bytes).map_err(|x| match *x {
            bincode::ErrorKind::Io(io) => io,
            _ => std::io::Error::new(std::io::ErrorKind::Other, "non-io error."),
        })
    }
}

impl Default for BinCodec {
    fn default() -> Self {
        Self {}
    }
}

#[tarpc::service]
pub trait GymRunner {
    async fn init(name: String, render: bool);
    async fn reset() -> SpaceData;
    async fn play(action: usize) -> State;
    async fn action_space() -> SpaceTemplate;
    async fn observation_space() -> SpaceTemplate;
}