<commit_before>use std;
use std::ffi;
use std::os::raw::{c_int, c_char, c_long, c_uchar, c_uint, c_ulong, c_void};
use std::ptr;

use x11::xlib;

use debug;
use keys::{KeyHandlers, KeyCombo, ModKey};

/// A handle to an X Window.
#[derive(Debug, PartialEq)]
pub struct WindowId(xlib::Window);

impl WindowId {
    fn to_x(&self) -> xlib::Window {
        self.0
    }
}

#[allow(non_snake_case)]
struct InternedAtoms {
    WM_DELETE_WINDOW: xlib::Atom,
    WM_PROTOCOLS: xlib::Atom,
}

pub struct Connection {
    display: *mut xlib::Display,
    root: WindowId,
    atoms: InternedAtoms,
}

// TODO: Implement Drop so that we can call XCloseDisplay?

impl Connection {
    /// Opens a connection to the X server, returning a new Connection object.
    pub fn connect() -> Result<Connection, String> {
        let (display, root) = unsafe {
            let display: *mut xlib::Display = xlib::XOpenDisplay(ptr::null_mut());
            let root: xlib::Window = xlib::XDefaultRootWindow(display);
            (display, root)
        };

        if display.is_null() {
            return Err("XOpenDisplay returned null pointer".to_owned());
        }
        if root == 0 {
            return Err("XDefaultRootWindow returned 0".to_owned());
        }

        Ok(Connection {
               display: display,
               root: WindowId(root),
               atoms: InternedAtoms {
                   WM_PROTOCOLS: Self::intern_atom(display, "WM_PROTOCOLS"),
                   WM_DELETE_WINDOW: Self::intern_atom(display, "WM_DELETE_WINDOW"),
               },
           })
    }

    /// Returns the Atom identifier associated with the atom_name str.
    fn intern_atom(display: *mut xlib::Display, atom_name: &str) -> xlib::Atom {
        // Note: the CString is bound to a variable to ensure adequate lifetime.
        let cstring = ffi::CString::new(atom_name).unwrap();
        let ptr = cstring.as_ptr() as *const c_char;
        unsafe { xlib::XInternAtom(display, ptr, 0) }
    }

    /// Installs the Connection as a window manager, by registers for SubstructureNotify and
    // SubstructureRedirect events on the root window. If there is already a window manager on the
    /// display, then this will fail.
    pub fn install_as_wm(&self) -> Result<(), String> {
        unsafe {
            // It's tricky to get state from the error handler to here, so we install a special
            // handler while becoming the WM that panics on error.
            xlib::XSetErrorHandler(Some(debug::error_handler_init));
            xlib::XSelectInput(self.display,
                               self.root.to_x(),
                               xlib::SubstructureNotifyMask | xlib::SubstructureRedirectMask);
            xlib::XSync(self.display, 0);

            // If we get this far, then our error handler didn't panic. Set a more useful error
            // handler.
            xlib::XSetErrorHandler(Some(debug::error_handler));
        }

        Ok(())
    }

    /// Returns the ID of the root window.
    pub fn root_window_id(&self) -> &WindowId {
        &self.root
    }

    /// Queries the WM_PROTOCOLS property of a window, returning a list of the protocols that it
    /// supports.
    // TODO: Have this return a list of atoms, rather than a list of strings. (Perhaps we should
    // have a separate function to convert to a list of strings for debugging?)
    pub fn get_wm_protocols(&self, window_id: &WindowId) -> Vec<String> {
        let mut atoms: *mut c_ulong = ptr::null_mut();
        let mut count: c_int = 0;
        unsafe {
            xlib::XGetWMProtocols(self.display, window_id.to_x(), &mut atoms, &mut count);
        }

        if atoms.is_null() {
            error!("XGetWMProtocols returned null pointer");
            return Vec::new();
        }
        if count == 0 {
            return Vec::new();
        }

        let mut pointers: Vec<*mut c_char> = Vec::with_capacity(count as usize);
        let protocols: Vec<String> = unsafe {
            xlib::XGetAtomNames(self.display, atoms, count, pointers.as_mut_ptr());
            pointers.set_len(count as usize);
            pointers.iter()
                .map(|buffer| ffi::CStr::from_ptr(*buffer).to_str().unwrap().to_owned())
                .collect()
        };

        unsafe {
            for pointer in pointers.iter() {
                xlib::XFree(*pointer as *mut c_void);
            }
            xlib::XFree(atoms as *mut c_void);
        }

        protocols
    }

    // TODO: This, as it'd make the WM_DELETE_WINDOW code a little clearer.
    // pub fn supports_protocol(&self, window_id, WindowId, atom: Atom) -> bool;

    /// Closes a window.
    ///
    /// The window will be closed gracefully using the ICCCM WM_DELETE_WINDOW protocol if it is
    /// supported.
    pub fn close_window(&self, window_id: &WindowId) {
        let protocols = self.get_wm_protocols(window_id);
        let has_wm_delete_window = protocols.contains(&"WM_DELETE_WINDOW".to_owned());

        // TODO: Use XDestroyWindow to forcefully close windows that do not support
        // WM_DELETE_WINDOW.
        if !has_wm_delete_window {
            panic!("Not implemented: closing windows that don't expose WM_DELETE_WINDOW");
        }

        let mut client_message = xlib::XClientMessageEvent {
            type_: xlib::ClientMessage,
            serial: 0,
            send_event: 0,
            display: ptr::null_mut(),
            window: window_id.to_x(),
            message_type: self.atoms.WM_PROTOCOLS,
            format: 32,
            data: xlib::ClientMessageData::new(),
        };
        client_message.data.set_long(0, self.atoms.WM_DELETE_WINDOW as c_long);
        client_message.data.set_long(1, xlib::CurrentTime as c_long);
        let mut event = xlib::XEvent::from(client_message);
        unsafe {
            xlib::XSendEvent(self.display,
                             window_id.to_x(),
                             0,
                             xlib::NoEventMask,
                             &mut event);
        }
    }

    /// Sets the window's position and size.
    pub fn configure_window(&self, window_id: &WindowId, x: i32, y: i32, width: i32, height: i32) {
        let mut changes = xlib::XWindowChanges {
            x: x,
            y: y,
            width: width,
            height: height,

            // Ignored:
            border_width: 0,
            sibling: 0,
            stack_mode: 0,
        };
        let flags = xlib::CWX | xlib::CWY | xlib::CWWidth | xlib::CWHeight;

        unsafe {
            xlib::XConfigureWindow(self.display, window_id.to_x(), flags as u32, &mut changes);
        };
    }

    /// Get's the window's width and height.
    pub fn get_window_geometry(&self, window_id: &WindowId) -> (i32, i32) {
        unsafe {
            let mut attrs: xlib::XWindowAttributes = std::mem::zeroed();
            xlib::XGetWindowAttributes(self.display, self.root.to_x(), &mut attrs);

            (attrs.width, attrs.height)
        }
    }

    /// Map a window.
    pub fn map_window(&self, window_id: &WindowId) {
        unsafe {
            xlib::XMapWindow(self.display, window_id.to_x());
        }
    }

    /// Registers for interesting events on the window.
    pub fn register_window_events(&self, window_id: &WindowId, key_handlers: &KeyHandlers) {
        unsafe {
            // Necessary in order to track which window is currently focused.
            xlib::XSelectInput(self.display, window_id.to_x(), xlib::EnterWindowMask);

            for key in key_handlers.key_combos() {
                let keycode = xlib::XKeysymToKeycode(self.display, key.keysym as u64) as i32;
                xlib::XGrabKey(self.display,
                               keycode,
                               key.mod_mask,
                               window_id.to_x(),
                               0,
                               xlib::GrabModeAsync,
                               xlib::GrabModeAsync);
            }
        }
    }

    pub fn focus_window(&self, window_id: &WindowId) {
        unsafe {
            xlib::XSetInputFocus(self.display,
                                 window_id.to_x(),
                                 xlib::RevertToPointerRoot,
                                 xlib::CurrentTime);
            // TODO: _NET_ACTIVE_WINDOW
        }
    }

    pub fn get_event_loop(&self) -> EventLoop {
        EventLoop { connection: &self }
    }
}

/// Events received from the `EventLoop`.
pub enum Event {
    MapRequest(WindowId),
    DestroyNotify(WindowId),
    KeyPress(KeyCombo),
    EnterNotify(WindowId),
}

/// An iterator that yields events from the X event loop.
///
/// Use `Connection::get_event_loop()` to get one.
pub struct EventLoop<'a> {
    connection: &'a Connection,
}

impl<'a> Iterator for EventLoop<'a> {
    type Item = Event;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            unsafe {
                let mut event = std::mem::zeroed();
                xlib::XNextEvent(self.connection.display, &mut event);
                debug!("Received event: {}", debug::xevent_to_str(&event));

                let event = match event.get_type() {
                    xlib::ConfigureRequest => {
                        self.on_configure_request(xlib::XConfigureRequestEvent::from(event))
                    }
                    // TODO: move most of these handlers up to the window manager. The only one
                    // that out to stay here is the ConfigureRequest, as we're not going to do much
                    // with it!
                    xlib::MapRequest => self.on_map_request(xlib::XMapRequestEvent::from(event)),
                    xlib::DestroyNotify => {
                        self.on_destroy_notify(xlib::XDestroyWindowEvent::from(event))
                    }
                    xlib::KeyPress => self.on_key_press(xlib::XKeyPressedEvent::from(event)),
                    xlib::EnterNotify => self.on_enter_notify(xlib::XEnterWindowEvent::from(event)),
                    _ => None,
                };

                if let Some(event) = event {
                    return Some(event);
                }
            }
        }
    }
}

impl<'a> EventLoop<'a> {
    fn on_configure_request(&self, event: xlib::XConfigureRequestEvent) -> Option<Event> {
        // This request is not interesting for us: grant it unchanged.
        let mut changes = xlib::XWindowChanges {
            x: event.x,
            y: event.y,
            width: event.width,
            height: event.height,
            border_width: event.border_width,
            sibling: event.above,
            stack_mode: event.detail,
        };

        unsafe {
            xlib::XConfigureWindow(self.connection.display,
                                   event.window,
                                   event.value_mask as u32,
                                   &mut changes);
        }

        // There's no value in propogating this event.
        None
    }

    fn on_map_request(&self, event: xlib::XMapRequestEvent) -> Option<Event> {
        Some(Event::MapRequest(WindowId(event.window)))
    }

    fn on_destroy_notify(&self, event: xlib::XDestroyWindowEvent) -> Option<Event> {
        Some(Event::DestroyNotify(WindowId(event.window)))
    }

    fn on_key_press(&self, event: xlib::XKeyPressedEvent) -> Option<Event> {
        let shift_index = if event.state & xlib::ShiftMask != 0 {
            1
        } else {
            0
        };
        let mod_mask = event.state & ModKey::mask_all();
        let keysym = unsafe {
            xlib::XKeycodeToKeysym(self.connection.display,
                                   event.keycode as c_uchar,
                                   shift_index)
        } as c_uint;
        let key = KeyCombo {
            mod_mask: mod_mask,
            keysym: keysym,
        };

        Some(Event::KeyPress(key))
    }

    fn on_enter_notify(&self, event: xlib::XEnterWindowEvent) -> Option<Event> {
        Some(Event::EnterNotify(WindowId(event.window)))
    }
}
<commit_msg>Get geometry of given WindowId, not root.<commit_after>use std;
use std::ffi;
use std::os::raw::{c_int, c_char, c_long, c_uchar, c_uint, c_ulong, c_void};
use std::ptr;

use x11::xlib;

use debug;
use keys::{KeyHandlers, KeyCombo, ModKey};

/// A handle to an X Window.
#[derive(Debug, PartialEq)]
pub struct WindowId(xlib::Window);

impl WindowId {
    fn to_x(&self) -> xlib::Window {
        self.0
    }
}

#[allow(non_snake_case)]
struct InternedAtoms {
    WM_DELETE_WINDOW: xlib::Atom,
    WM_PROTOCOLS: xlib::Atom,
}

pub struct Connection {
    display: *mut xlib::Display,
    root: WindowId,
    atoms: InternedAtoms,
}

// TODO: Implement Drop so that we can call XCloseDisplay?

impl Connection {
    /// Opens a connection to the X server, returning a new Connection object.
    pub fn connect() -> Result<Connection, String> {
        let (display, root) = unsafe {
            let display: *mut xlib::Display = xlib::XOpenDisplay(ptr::null_mut());
            let root: xlib::Window = xlib::XDefaultRootWindow(display);
            (display, root)
        };

        if display.is_null() {
            return Err("XOpenDisplay returned null pointer".to_owned());
        }
        if root == 0 {
            return Err("XDefaultRootWindow returned 0".to_owned());
        }

        Ok(Connection {
               display: display,
               root: WindowId(root),
               atoms: InternedAtoms {
                   WM_PROTOCOLS: Self::intern_atom(display, "WM_PROTOCOLS"),
                   WM_DELETE_WINDOW: Self::intern_atom(display, "WM_DELETE_WINDOW"),
               },
           })
    }

    /// Returns the Atom identifier associated with the atom_name str.
    fn intern_atom(display: *mut xlib::Display, atom_name: &str) -> xlib::Atom {
        // Note: the CString is bound to a variable to ensure adequate lifetime.
        let cstring = ffi::CString::new(atom_name).unwrap();
        let ptr = cstring.as_ptr() as *const c_char;
        unsafe { xlib::XInternAtom(display, ptr, 0) }
    }

    /// Installs the Connection as a window manager, by registers for SubstructureNotify and
    // SubstructureRedirect events on the root window. If there is already a window manager on the
    /// display, then this will fail.
    pub fn install_as_wm(&self) -> Result<(), String> {
        unsafe {
            // It's tricky to get state from the error handler to here, so we install a special
            // handler while becoming the WM that panics on error.
            xlib::XSetErrorHandler(Some(debug::error_handler_init));
            xlib::XSelectInput(self.display,
                               self.root.to_x(),
                               xlib::SubstructureNotifyMask | xlib::SubstructureRedirectMask);
            xlib::XSync(self.display, 0);

            // If we get this far, then our error handler didn't panic. Set a more useful error
            // handler.
            xlib::XSetErrorHandler(Some(debug::error_handler));
        }

        Ok(())
    }

    /// Returns the ID of the root window.
    pub fn root_window_id(&self) -> &WindowId {
        &self.root
    }

    /// Queries the WM_PROTOCOLS property of a window, returning a list of the protocols that it
    /// supports.
    // TODO: Have this return a list of atoms, rather than a list of strings. (Perhaps we should
    // have a separate function to convert to a list of strings for debugging?)
    pub fn get_wm_protocols(&self, window_id: &WindowId) -> Vec<String> {
        let mut atoms: *mut c_ulong = ptr::null_mut();
        let mut count: c_int = 0;
        unsafe {
            xlib::XGetWMProtocols(self.display, window_id.to_x(), &mut atoms, &mut count);
        }

        if atoms.is_null() {
            error!("XGetWMProtocols returned null pointer");
            return Vec::new();
        }
        if count == 0 {
            return Vec::new();
        }

        let mut pointers: Vec<*mut c_char> = Vec::with_capacity(count as usize);
        let protocols: Vec<String> = unsafe {
            xlib::XGetAtomNames(self.display, atoms, count, pointers.as_mut_ptr());
            pointers.set_len(count as usize);
            pointers.iter()
                .map(|buffer| ffi::CStr::from_ptr(*buffer).to_str().unwrap().to_owned())
                .collect()
        };

        unsafe {
            for pointer in pointers.iter() {
                xlib::XFree(*pointer as *mut c_void);
            }
            xlib::XFree(atoms as *mut c_void);
        }

        protocols
    }

    // TODO: This, as it'd make the WM_DELETE_WINDOW code a little clearer.
    // pub fn supports_protocol(&self, window_id, WindowId, atom: Atom) -> bool;

    /// Closes a window.
    ///
    /// The window will be closed gracefully using the ICCCM WM_DELETE_WINDOW protocol if it is
    /// supported.
    pub fn close_window(&self, window_id: &WindowId) {
        let protocols = self.get_wm_protocols(window_id);
        let has_wm_delete_window = protocols.contains(&"WM_DELETE_WINDOW".to_owned());

        // TODO: Use XDestroyWindow to forcefully close windows that do not support
        // WM_DELETE_WINDOW.
        if !has_wm_delete_window {
            panic!("Not implemented: closing windows that don't expose WM_DELETE_WINDOW");
        }

        let mut client_message = xlib::XClientMessageEvent {
            type_: xlib::ClientMessage,
            serial: 0,
            send_event: 0,
            display: ptr::null_mut(),
            window: window_id.to_x(),
            message_type: self.atoms.WM_PROTOCOLS,
            format: 32,
            data: xlib::ClientMessageData::new(),
        };
        client_message.data.set_long(0, self.atoms.WM_DELETE_WINDOW as c_long);
        client_message.data.set_long(1, xlib::CurrentTime as c_long);
        let mut event = xlib::XEvent::from(client_message);
        unsafe {
            xlib::XSendEvent(self.display,
                             window_id.to_x(),
                             0,
                             xlib::NoEventMask,
                             &mut event);
        }
    }

    /// Sets the window's position and size.
    pub fn configure_window(&self, window_id: &WindowId, x: i32, y: i32, width: i32, height: i32) {
        let mut changes = xlib::XWindowChanges {
            x: x,
            y: y,
            width: width,
            height: height,

            // Ignored:
            border_width: 0,
            sibling: 0,
            stack_mode: 0,
        };
        let flags = xlib::CWX | xlib::CWY | xlib::CWWidth | xlib::CWHeight;

        unsafe {
            xlib::XConfigureWindow(self.display, window_id.to_x(), flags as u32, &mut changes);
        };
    }

    /// Get's the window's width and height.
    pub fn get_window_geometry(&self, window_id: &WindowId) -> (i32, i32) {
        unsafe {
            let mut attrs: xlib::XWindowAttributes = std::mem::zeroed();
            xlib::XGetWindowAttributes(self.display, window_id.to_x(), &mut attrs);

            (attrs.width, attrs.height)
        }
    }

    /// Map a window.
    pub fn map_window(&self, window_id: &WindowId) {
        unsafe {
            xlib::XMapWindow(self.display, window_id.to_x());
        }
    }

    /// Registers for interesting events on the window.
    pub fn register_window_events(&self, window_id: &WindowId, key_handlers: &KeyHandlers) {
        unsafe {
            // Necessary in order to track which window is currently focused.
            xlib::XSelectInput(self.display, window_id.to_x(), xlib::EnterWindowMask);

            for key in key_handlers.key_combos() {
                let keycode = xlib::XKeysymToKeycode(self.display, key.keysym as u64) as i32;
                xlib::XGrabKey(self.display,
                               keycode,
                               key.mod_mask,
                               window_id.to_x(),
                               0,
                               xlib::GrabModeAsync,
                               xlib::GrabModeAsync);
            }
        }
    }

    pub fn focus_window(&self, window_id: &WindowId) {
        unsafe {
            xlib::XSetInputFocus(self.display,
                                 window_id.to_x(),
                                 xlib::RevertToPointerRoot,
                                 xlib::CurrentTime);
            // TODO: _NET_ACTIVE_WINDOW
        }
    }

    pub fn get_event_loop(&self) -> EventLoop {
        EventLoop { connection: &self }
    }
}

/// Events received from the `EventLoop`.
pub enum Event {
    MapRequest(WindowId),
    DestroyNotify(WindowId),
    KeyPress(KeyCombo),
    EnterNotify(WindowId),
}

/// An iterator that yields events from the X event loop.
///
/// Use `Connection::get_event_loop()` to get one.
pub struct EventLoop<'a> {
    connection: &'a Connection,
}

impl<'a> Iterator for EventLoop<'a> {
    type Item = Event;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            unsafe {
                let mut event = std::mem::zeroed();
                xlib::XNextEvent(self.connection.display, &mut event);
                debug!("Received event: {}", debug::xevent_to_str(&event));

                let event = match event.get_type() {
                    xlib::ConfigureRequest => {
                        self.on_configure_request(xlib::XConfigureRequestEvent::from(event))
                    }
                    // TODO: move most of these handlers up to the window manager. The only one
                    // that out to stay here is the ConfigureRequest, as we're not going to do much
                    // with it!
                    xlib::MapRequest => self.on_map_request(xlib::XMapRequestEvent::from(event)),
                    xlib::DestroyNotify => {
                        self.on_destroy_notify(xlib::XDestroyWindowEvent::from(event))
                    }
                    xlib::KeyPress => self.on_key_press(xlib::XKeyPressedEvent::from(event)),
                    xlib::EnterNotify => self.on_enter_notify(xlib::XEnterWindowEvent::from(event)),
                    _ => None,
                };

                if let Some(event) = event {
                    return Some(event);
                }
            }
        }
    }
}

impl<'a> EventLoop<'a> {
    fn on_configure_request(&self, event: xlib::XConfigureRequestEvent) -> Option<Event> {
        // This request is not interesting for us: grant it unchanged.
        let mut changes = xlib::XWindowChanges {
            x: event.x,
            y: event.y,
            width: event.width,
            height: event.height,
            border_width: event.border_width,
            sibling: event.above,
            stack_mode: event.detail,
        };

        unsafe {
            xlib::XConfigureWindow(self.connection.display,
                                   event.window,
                                   event.value_mask as u32,
                                   &mut changes);
        }

        // There's no value in propogating this event.
        None
    }

    fn on_map_request(&self, event: xlib::XMapRequestEvent) -> Option<Event> {
        Some(Event::MapRequest(WindowId(event.window)))
    }

    fn on_destroy_notify(&self, event: xlib::XDestroyWindowEvent) -> Option<Event> {
        Some(Event::DestroyNotify(WindowId(event.window)))
    }

    fn on_key_press(&self, event: xlib::XKeyPressedEvent) -> Option<Event> {
        let shift_index = if event.state & xlib::ShiftMask != 0 {
            1
        } else {
            0
        };
        let mod_mask = event.state & ModKey::mask_all();
        let keysym = unsafe {
            xlib::XKeycodeToKeysym(self.connection.display,
                                   event.keycode as c_uchar,
                                   shift_index)
        } as c_uint;
        let key = KeyCombo {
            mod_mask: mod_mask,
            keysym: keysym,
        };

        Some(Event::KeyPress(key))
    }

    fn on_enter_notify(&self, event: xlib::XEnterWindowEvent) -> Option<Event> {
        Some(Event::EnterNotify(WindowId(event.window)))
    }
}
<|endoftext|>


<commit_before>use crate::{
    core::*,
    domains::{Domain, Observation},
    geometry::Space,
};
use rand::{thread_rng, Rng};
use slog::{Logger, Record, Result as LogResult, Serializer, KV};

/// Container for episodic statistics.
#[derive(Debug)]
pub struct Episode {
    /// The number of steps taken to reach the terminal state.
    pub steps: u64,

    /// The total accumulated reward over the episode.
    pub reward: f64,
}

impl KV for Episode {
    fn serialize(&self, _: &Record, serializer: &mut Serializer) -> LogResult {
        serializer.emit_u64("steps", self.steps)?;
        serializer.emit_f64("reward", self.reward)?;

        Ok(())
    }
}

/// Helper function for running experiments.
pub fn run(
    runner: impl Iterator<Item = Episode>,
    n_episodes: usize,
    logger: Option<Logger>,
) -> Vec<Episode>
{
    let exp = runner.take(n_episodes);

    match logger {
        Some(logger) => exp
            .zip(1..(n_episodes + 1))
            .inspect(|&(ref res, i)| {
                info!(logger, "episode {}", i; res);
            })
            .map(|(res, _)| res)
            .collect(),

        None => exp.collect(),
    }
}

/// Utility for running a single evaluation episode.
pub struct Evaluation<'a, C: 'a, D> {
    agent: &'a mut C,
    domain_factory: Box<Fn() -> D>,
}

impl<'a, S: Space, A: Space, C, D> Evaluation<'a, C, D>
where
    C: Controller<S::Value, A::Value>,
    D: Domain<StateSpace = S, ActionSpace = A>,
{
    pub fn new(agent: &'a mut C, domain_factory: Box<Fn() -> D>) -> Evaluation<'a, C, D> {
        Evaluation {
            agent,
            domain_factory,
        }
    }
}

impl<'a, S: Space, A: Space, C, D> Iterator for Evaluation<'a, C, D>
where
    C: Controller<S::Value, A::Value>,
    D: Domain<StateSpace = S, ActionSpace = A>,
{
    type Item = Episode;

    fn next(&mut self) -> Option<Episode> {
        let mut rng = thread_rng();
        let mut domain = (self.domain_factory)();
        let mut a = self.agent.sample_target(&mut rng, domain.emit().state());

        let mut e = Episode {
            steps: 1,
            reward: 0.0,
        };

        loop {
            let t = domain.step(a);

            e.steps += 1;
            e.reward += t.reward;

            a = match t.to {
                Observation::Terminal(_) => break,
                _ => self.agent.sample_target(&mut rng, t.to.state()),
            };
        }

        Some(e)
    }
}

/// Utility for running a sequence of training episodes.
pub struct SerialExperiment<'a, C: 'a, D> {
    agent: &'a mut C,
    domain_factory: Box<Fn() -> D>,

    step_limit: u64,
}

impl<'a, S: Space, A: Space, C, D> SerialExperiment<'a, C, D>
where
    C: Controller<S::Value, A::Value>,
    D: Domain<StateSpace = S, ActionSpace = A>,
{
    pub fn new(
        agent: &'a mut C,
        domain_factory: Box<Fn() -> D>,
        step_limit: u64,
    ) -> SerialExperiment<'a, C, D>
    {
        SerialExperiment {
            agent,
            domain_factory,
            step_limit,
        }
    }
}

impl<'a, S: Space, A: Space, C, D> Iterator for SerialExperiment<'a, C, D>
where
    C: OnlineLearner<S::Value, A::Value> + Controller<S::Value, A::Value>,
    D: Domain<StateSpace = S, ActionSpace = A>,
{
    type Item = Episode;

    fn next(&mut self) -> Option<Episode> {
        let mut rng = thread_rng();
        let mut domain = (self.domain_factory)();
        let mut a = self.agent.sample_behaviour(&mut rng, domain.emit().state());

        let mut e = Episode {
            steps: 1,
            reward: 0.0,
        };

        for j in 1..(self.step_limit + 1) {
            let t = domain.step(a);

            e.steps = j;
            e.reward += t.reward;

            self.agent.handle_transition(&t);

            if t.terminated() || j >= self.step_limit {
                self.agent.handle_terminal();

                break
            } else {
                a = self.agent.sample_behaviour(&mut rng, t.to.state());
            }
        }

        Some(e)
    }
}
<commit_msg>feat(core): prepend dyn to dynamic trait object type definitions<commit_after>use crate::{
    core::*,
    domains::{Domain, Observation},
    geometry::Space,
};
use rand::{thread_rng, Rng};
use slog::{Logger, Record, Result as LogResult, Serializer, KV};

/// Container for episodic statistics.
#[derive(Debug, Clone)]
pub struct Episode {
    /// The number of steps taken to reach the terminal state.
    pub steps: u64,

    /// The total accumulated reward over the episode.
    pub reward: f64,
}

impl KV for Episode {
    fn serialize(&self, _: &Record, serializer: &mut dyn Serializer) -> LogResult {
        serializer.emit_u64("steps", self.steps)?;
        serializer.emit_f64("reward", self.reward)?;

        Ok(())
    }
}

/// Helper function for running experiments.
pub fn run(
    runner: impl Iterator<Item = Episode>,
    n_episodes: usize,
    logger: Option<Logger>,
) -> Vec<Episode>
{
    let exp = runner.take(n_episodes);

    match logger {
        Some(logger) => exp
            .zip(1..(n_episodes + 1))
            .inspect(|&(ref res, i)| {
                info!(logger, "episode {}", i; res);
            })
            .map(|(res, _)| res)
            .collect(),

        None => exp.collect(),
    }
}

/// Utility for running a single evaluation episode.
pub struct Evaluation<'a, C: 'a, D> {
    agent: &'a mut C,
    domain_factory: Box<dyn Fn() -> D>,
}

impl<'a, S: Space, A: Space, C, D> Evaluation<'a, C, D>
where
    C: Controller<S::Value, A::Value>,
    D: Domain<StateSpace = S, ActionSpace = A>,
{
    pub fn new(agent: &'a mut C, domain_factory: Box<dyn Fn() -> D>) -> Evaluation<'a, C, D> {
        Evaluation {
            agent,
            domain_factory,
        }
    }
}

impl<'a, S: Space, A: Space, C, D> Iterator for Evaluation<'a, C, D>
where
    C: Controller<S::Value, A::Value>,
    D: Domain<StateSpace = S, ActionSpace = A>,
{
    type Item = Episode;

    fn next(&mut self) -> Option<Episode> {
        let mut rng = thread_rng();
        let mut domain = (self.domain_factory)();
        let mut a = self.agent.sample_target(&mut rng, domain.emit().state());

        let mut e = Episode {
            steps: 1,
            reward: 0.0,
        };

        loop {
            let t = domain.step(a);

            e.steps += 1;
            e.reward += t.reward;

            a = match t.to {
                Observation::Terminal(_) => break,
                _ => self.agent.sample_target(&mut rng, t.to.state()),
            };
        }

        Some(e)
    }
}

/// Utility for running a sequence of training episodes.
pub struct SerialExperiment<'a, C: 'a, D> {
    agent: &'a mut C,
    domain_factory: Box<dyn Fn() -> D>,

    step_limit: u64,
}

impl<'a, S: Space, A: Space, C, D> SerialExperiment<'a, C, D>
where
    C: Controller<S::Value, A::Value>,
    D: Domain<StateSpace = S, ActionSpace = A>,
{
    pub fn new(
        agent: &'a mut C,
        domain_factory: Box<dyn Fn() -> D>,
        step_limit: u64,
    ) -> SerialExperiment<'a, C, D>
    {
        SerialExperiment {
            agent,
            domain_factory,
            step_limit,
        }
    }
}

impl<'a, S: Space, A: Space, C, D> Iterator for SerialExperiment<'a, C, D>
where
    C: OnlineLearner<S::Value, A::Value> + Controller<S::Value, A::Value>,
    D: Domain<StateSpace = S, ActionSpace = A>,
{
    type Item = Episode;

    fn next(&mut self) -> Option<Episode> {
        let mut rng = thread_rng();
        let mut domain = (self.domain_factory)();
        let mut a = self.agent.sample_behaviour(&mut rng, domain.emit().state());

        let mut e = Episode {
            steps: 1,
            reward: 0.0,
        };

        for j in 1..(self.step_limit + 1) {
            let t = domain.step(a);

            e.steps = j;
            e.reward += t.reward;

            self.agent.handle_transition(&t);

            if t.terminated() || j >= self.step_limit {
                self.agent.handle_terminal();

                break
            } else {
                a = self.agent.sample_behaviour(&mut rng, t.to.state());
            }
        }

        Some(e)
    }
}
<|endoftext|>


<commit_before>use ast;
use lexer;
use parser;

use interp::env;
use interp::types;

use core::hashmap::linear;
use core::to_bytes;

/*
 * aster - AST EvaulatoR
 *
 * aster evaluates a Prismo program via reduction of the AST.
 *
 * "It's pretty terrible."
 * -- Tony, 2013
 */

impl to_bytes::IterBytes for ast::RecordName {
    fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) {
        match self {
            &ast::RecordName(n, s) => (n, s).iter_bytes(lsb0, f)
        }
    }
}

impl to_bytes::IterBytes for ast::DottedName {
    fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) {
        match self {
            &ast::DottedName(s) => s.iter_bytes(lsb0, f)
        }
    }
}

pub struct Interp {
    record_types: @mut linear::LinearMap<@ast::RecordName, @ast::RecordDeclaration>,
    import_paths: @[Path],
    root: @mut env::Env<Interp>,
    argv: @[@str],
    filename: @str,
    current_frame: @mut option::Option<@mut Frame>
}

pub impl Interp {
    pub fn new(import_paths: @[Path], argv: @[@str], filename: @str) -> Interp {
        Interp {
            record_types: @mut linear::LinearMap::new(),
            import_paths: import_paths,
            root: @mut env::Env::new(),
            argv: argv,
            filename: filename,
            current_frame: @mut option::None
        }
    }
}

fn wind(interp: @mut Interp, env: @mut env::Env<Interp>, lineno: uint) -> @mut Frame {
    // allocate a new frame
    let frame = @mut Frame::new(interp, env, interp.filename, lineno);
    interp.current_frame = @mut option::Some(frame);
    frame
}

fn unwind(interp: @mut Interp) -> () {
    interp.current_frame = match *interp.current_frame {
        option::None => @mut option::None,
        option::Some(frame) => frame.parent
    };
}

pub struct Frame {
    parent: @mut option::Option<@mut Frame>,
    env: @mut env::Env<Interp>,
    file: @str,
    lineno: uint,
    exception: @mut option::Option<@mut types::Val<Interp>>
}

pub impl Frame {
    pub fn new(interp: @mut Interp, env: @mut env::Env<Interp>, file: @str, lineno: uint) -> Frame {
        Frame {
            parent: interp.current_frame,
            env: env,
            file: file,
            lineno: lineno,
            exception: @mut option::None
        }
    }
}

pub fn import_module(interp: @mut Interp, name: &ast::DottedName, qualified: bool) -> () {
    let module_parts = (**name).to_owned().map(|x| (*x).to_owned());
    let mut parts = copy module_parts;

    vec::reverse(parts);
    let module_name = copy parts[0];
    let filename = fmt!("%s.pr", module_name);
    parts = vec::tail(parts).to_owned();
    vec::reverse(parts);

    let mut found = false;
    let mut paths = interp.import_paths.to_owned();

    for paths.each_mut |import_path| {
        // make a path inside the import path and check if that exists
        let base_path = (copy *import_path).push_many(parts);
        let path = base_path.push(filename);

        if os::path_exists(&path) {
            // make a copy of the import paths then give them to a new sub-interpreter
            let mut import_paths1 = interp.import_paths.to_owned();
            import_paths1.unshift(base_path);

            let interp1 = @mut Interp::new(at_vec::from_owned(import_paths1), interp.argv, path.to_str().to_managed());

            // run the file in the sub-interpreter
            run_file(interp1, &path);

            if qualified {
                let module_name = ast::Sym(module_name.to_managed());

                // qualified imports symbols from child interpreter into a module
                env::declare(interp.root, &module_name,
                             @mut types::Module(interp1.root.vars));

                for interp1.record_types.each_key |k| {
                    interp.record_types.insert(match *k {
                        @ast::RecordName(modu, name) => @ast::RecordName(@ast::DottedName(**modu + [module_name]), name)
                    }, *interp1.record_types.find(k).unwrap());
                }
            } else {
                // otherwise we just plop all the symbols in
                for interp1.root.vars.each_key |k| {
                    env::declare(interp.root, k, *interp1.root.vars.find(k).unwrap());
                }

                // import record types
                for interp1.record_types.each_key |k| {
                    interp.record_types.insert(*k, *interp1.record_types.find(k).unwrap());
                }
            }

            found = true;
            break;
        }
    };

    if !found {
        fail!(fmt!("couldn't import module %s", str::connect(module_parts, ".")))
    };
}

fn unify_pattern_basic(interp: @mut Interp, env: @mut env::Env<Interp>, pat: &ast::Pat, val: @mut types::Val<Interp>) -> bool {
    // perform pattern matching of basic patterns -- if a match fails, we leave all the symbols in
    // scope and not care. this is pretty bad.
    match *pat {
        ast::AnyPattern => true,
        ast::ManyPattern => false,
        ast::UnitPattern => match *val {
            types::Unit => true,
            _ => false
        },
        ast::LiteralPattern(lit) => *val == types:: gen_lit(lit),
        ast::SymbolPattern(sym) => {
            env::declare(env, &sym, val);
            true
        },
        ast::BoundPattern(sym, pat) => {
            let r = unify_pattern_basic(interp, env, pat, val);
            env::declare(env, &sym, val);
            r
        },
        ast::DisjunctivePattern(pat1, pat2) => {
            unify_pattern_basic(interp, env, pat1, val) || unify_pattern_basic(interp, env, pat2, val)
        },
        ast::ConjunctivePattern(pat1, pat2) => {
            unify_pattern_basic(interp, env, pat1, val) && unify_pattern_basic(interp, env, pat2, val)
        },
        ast::RecordPattern(pat_decl, pats) => fail!(~":V"),
        ast::ListPattern(pats) => match *val {
            types::List(vals) => {
                if vals.len() < pats.len() {
                    false
                } else {
                    match pats[pats.len() - 1] {
                        ast::ManyPattern => {
                            // pattern match with a spread argument
                            let mut ok = true;
                            let mut i = 0;
                            while i < pats.len() - 1 {
                                ok = unify_pattern_basic(interp, env, &pats[i], vals[i]);
                                if !ok { break; };
                                i += 1;
                            }
                            ok
                        },
                        _ => {
                            if vals.len() > pats.len() {
                                // no spread argument, :(
                                false
                            } else {
                                // pattern match with a full pattern list
                                let mut ok = true;
                                let mut i = 0;
                                while i < pats.len() {
                                    ok = unify_pattern_basic(interp, env, &pats[i], vals[i]);
                                    if !ok { break; };
                                    i += 1;
                                }
                                ok
                            }
                        }
                    }
                }
            },
            _ => false
        }
    }
}

fn unify_pattern_let(interp: @mut Interp, env: @mut env::Env<Interp>, pat: &ast::LetPat, val: @mut types::Val<Interp>) -> bool {
    match *pat {
        ast::BasicPattern(pat) => unify_pattern_basic(interp, env, &pat, val),
        _ => fail!(~":V")
    }
}

fn eval_exp(interp: @mut Interp, env: @mut env::Env<Interp>, exp: &ast::Exp) -> types::Val<Interp> {
    let frame = interp.current_frame.unwrap();

    let r = match exp.exp {
        ast::UnitExpression => types::Unit,
        ast::LiteralExpression(l) => types::gen_lit(l),
        ast::LambdaExpression(pat, exp) => types::Function(@[types::Fun {
            pattern: pat,
            body: exp,
            env: env
        }]),
        ast::ListExpression(exps) => {
            let mut vals = @[];

            for exps.each |exp| {
                vals += [@mut eval_exp(interp, env, exp)];
                if !frame.exception.is_none() {
                    return types::Unit;
                }
            }

            types::List(vals)
        },
        ast::SymbolExpression(sym) => {
            match env::find(env, &sym) {
                option::Some(x) => types::Unit,
                option::None => {
                    frame.exception = @mut option::Some(@mut types::String(fmt!("symbol `%s` not found in scope", *sym).to_managed()));
                    types::Unit
                }
            }
        },
        _ => fail!(~":V")
    };

    r
}

fn exec_stmt(interp: @mut Interp, env: @mut env::Env<Interp>, stmt: &ast::Stmt) -> () {
    let frame = interp.current_frame.unwrap();

    match stmt.stmt {
        ast::LetBindingStatement(pat, exp) => {
            if !unify_pattern_let(interp, env, &pat, @mut eval_exp(interp, env, &exp)) {
                // uh oh, we need to throw an exception and unwind
                frame.exception = @mut option::Some(@mut types::String(@"pattern match failed"));
            }
        },
        ast::ExpressionStatement(exp) => { eval_exp(interp, env, &exp); }
    };
}

pub fn run_file(interp: @mut Interp, path: &Path) -> () {
    match io::file_reader(path) {
        result::Ok(r) => {
            let tokens = lexer::lex(r);
            let program = parser::parse(tokens);
            run(interp, program);
        },
        result::Err(f) => fail!(f)
    }
}

pub fn run(interp: @mut Interp, prog: ast::Program) -> () {
    for prog.imports.each |imp| {
        import_module(interp, &imp.module, imp.qualified);
    }

    for prog.records.each |rec| {
        interp.record_types.insert(@rec.name, @*rec);
    }

    for prog.body.each |stmt| {
        let mut frame = wind(interp, interp.root, stmt.lineno);

        exec_stmt(interp, interp.root, stmt);

        match *frame.exception {
            option::None => unwind(interp),
            option::Some(f) => {
                fail!(fmt!("%? in %s:%u", f, frame.file, frame.lineno))
            }
        }
    }
}
<commit_msg>pattern failed -> refuted<commit_after>use ast;
use lexer;
use parser;

use interp::env;
use interp::types;

use core::hashmap::linear;
use core::to_bytes;

/*
 * aster - AST EvaulatoR
 *
 * aster evaluates a Prismo program via reduction of the AST.
 *
 * "It's pretty terrible."
 * -- Tony, 2013
 */

impl to_bytes::IterBytes for ast::RecordName {
    fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) {
        match self {
            &ast::RecordName(n, s) => (n, s).iter_bytes(lsb0, f)
        }
    }
}

impl to_bytes::IterBytes for ast::DottedName {
    fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) {
        match self {
            &ast::DottedName(s) => s.iter_bytes(lsb0, f)
        }
    }
}

pub struct Interp {
    record_types: @mut linear::LinearMap<@ast::RecordName, @ast::RecordDeclaration>,
    import_paths: @[Path],
    root: @mut env::Env<Interp>,
    argv: @[@str],
    filename: @str,
    current_frame: @mut option::Option<@mut Frame>
}

pub impl Interp {
    pub fn new(import_paths: @[Path], argv: @[@str], filename: @str) -> Interp {
        Interp {
            record_types: @mut linear::LinearMap::new(),
            import_paths: import_paths,
            root: @mut env::Env::new(),
            argv: argv,
            filename: filename,
            current_frame: @mut option::None
        }
    }
}

fn wind(interp: @mut Interp, env: @mut env::Env<Interp>, lineno: uint) -> @mut Frame {
    // allocate a new frame
    let frame = @mut Frame::new(interp, env, interp.filename, lineno);
    interp.current_frame = @mut option::Some(frame);
    frame
}

fn unwind(interp: @mut Interp) -> () {
    interp.current_frame = match *interp.current_frame {
        option::None => @mut option::None,
        option::Some(frame) => frame.parent
    };
}

pub struct Frame {
    parent: @mut option::Option<@mut Frame>,
    env: @mut env::Env<Interp>,
    file: @str,
    lineno: uint,
    exception: @mut option::Option<@mut types::Val<Interp>>
}

pub impl Frame {
    pub fn new(interp: @mut Interp, env: @mut env::Env<Interp>, file: @str, lineno: uint) -> Frame {
        Frame {
            parent: interp.current_frame,
            env: env,
            file: file,
            lineno: lineno,
            exception: @mut option::None
        }
    }
}

pub fn import_module(interp: @mut Interp, name: &ast::DottedName, qualified: bool) -> () {
    let module_parts = (**name).to_owned().map(|x| (*x).to_owned());
    let mut parts = copy module_parts;

    vec::reverse(parts);
    let module_name = copy parts[0];
    let filename = fmt!("%s.pr", module_name);
    parts = vec::tail(parts).to_owned();
    vec::reverse(parts);

    let mut found = false;
    let mut paths = interp.import_paths.to_owned();

    for paths.each_mut |import_path| {
        // make a path inside the import path and check if that exists
        let base_path = (copy *import_path).push_many(parts);
        let path = base_path.push(filename);

        if os::path_exists(&path) {
            // make a copy of the import paths then give them to a new sub-interpreter
            let mut import_paths1 = interp.import_paths.to_owned();
            import_paths1.unshift(base_path);

            let interp1 = @mut Interp::new(at_vec::from_owned(import_paths1), interp.argv, path.to_str().to_managed());

            // run the file in the sub-interpreter
            run_file(interp1, &path);

            if qualified {
                let module_name = ast::Sym(module_name.to_managed());

                // qualified imports symbols from child interpreter into a module
                env::declare(interp.root, &module_name,
                             @mut types::Module(interp1.root.vars));

                for interp1.record_types.each_key |k| {
                    interp.record_types.insert(match *k {
                        @ast::RecordName(modu, name) => @ast::RecordName(@ast::DottedName(**modu + [module_name]), name)
                    }, *interp1.record_types.find(k).unwrap());
                }
            } else {
                // otherwise we just plop all the symbols in
                for interp1.root.vars.each_key |k| {
                    env::declare(interp.root, k, *interp1.root.vars.find(k).unwrap());
                }

                // import record types
                for interp1.record_types.each_key |k| {
                    interp.record_types.insert(*k, *interp1.record_types.find(k).unwrap());
                }
            }

            found = true;
            break;
        }
    };

    if !found {
        fail!(fmt!("couldn't import module %s", str::connect(module_parts, ".")))
    };
}

fn unify_pattern_basic(interp: @mut Interp, env: @mut env::Env<Interp>, pat: &ast::Pat, val: @mut types::Val<Interp>) -> bool {
    // perform pattern matching of basic patterns -- if a match fails, we leave all the symbols in
    // scope and not care. this is pretty bad.
    match *pat {
        ast::AnyPattern => true,
        ast::ManyPattern => false,
        ast::UnitPattern => match *val {
            types::Unit => true,
            _ => false
        },
        ast::LiteralPattern(lit) => *val == types:: gen_lit(lit),
        ast::SymbolPattern(sym) => {
            env::declare(env, &sym, val);
            true
        },
        ast::BoundPattern(sym, pat) => {
            let r = unify_pattern_basic(interp, env, pat, val);
            env::declare(env, &sym, val);
            r
        },
        ast::DisjunctivePattern(pat1, pat2) => {
            unify_pattern_basic(interp, env, pat1, val) || unify_pattern_basic(interp, env, pat2, val)
        },
        ast::ConjunctivePattern(pat1, pat2) => {
            unify_pattern_basic(interp, env, pat1, val) && unify_pattern_basic(interp, env, pat2, val)
        },
        ast::RecordPattern(pat_decl, pats) => fail!(~":V"),
        ast::ListPattern(pats) => match *val {
            types::List(vals) => {
                if vals.len() < pats.len() {
                    false
                } else {
                    match pats[pats.len() - 1] {
                        ast::ManyPattern => {
                            // pattern match with a spread argument
                            let mut ok = true;
                            let mut i = 0;
                            while i < pats.len() - 1 {
                                ok = unify_pattern_basic(interp, env, &pats[i], vals[i]);
                                if !ok { break; };
                                i += 1;
                            }
                            ok
                        },
                        _ => {
                            if vals.len() > pats.len() {
                                // no spread argument, :(
                                false
                            } else {
                                // pattern match with a full pattern list
                                let mut ok = true;
                                let mut i = 0;
                                while i < pats.len() {
                                    ok = unify_pattern_basic(interp, env, &pats[i], vals[i]);
                                    if !ok { break; };
                                    i += 1;
                                }
                                ok
                            }
                        }
                    }
                }
            },
            _ => false
        }
    }
}

fn unify_pattern_let(interp: @mut Interp, env: @mut env::Env<Interp>, pat: &ast::LetPat, val: @mut types::Val<Interp>) -> bool {
    match *pat {
        ast::BasicPattern(pat) => unify_pattern_basic(interp, env, &pat, val),
        _ => fail!(~":V")
    }
}

fn eval_exp(interp: @mut Interp, env: @mut env::Env<Interp>, exp: &ast::Exp) -> types::Val<Interp> {
    let frame = interp.current_frame.unwrap();

    let r = match exp.exp {
        ast::UnitExpression => types::Unit,
        ast::LiteralExpression(l) => types::gen_lit(l),
        ast::LambdaExpression(pat, exp) => types::Function(@[types::Fun {
            pattern: pat,
            body: exp,
            env: env
        }]),
        ast::ListExpression(exps) => {
            let mut vals = @[];

            for exps.each |exp| {
                vals += [@mut eval_exp(interp, env, exp)];
                if !frame.exception.is_none() {
                    return types::Unit;
                }
            }

            types::List(vals)
        },
        ast::SymbolExpression(sym) => {
            match env::find(env, &sym) {
                option::Some(x) => types::Unit,
                option::None => {
                    frame.exception = @mut option::Some(@mut types::String(fmt!("symbol `%s` not found in scope", *sym).to_managed()));
                    types::Unit
                }
            }
        },
        _ => fail!(~":V")
    };

    r
}

fn exec_stmt(interp: @mut Interp, env: @mut env::Env<Interp>, stmt: &ast::Stmt) -> () {
    let frame = interp.current_frame.unwrap();

    match stmt.stmt {
        ast::LetBindingStatement(pat, exp) => {
            if !unify_pattern_let(interp, env, &pat, @mut eval_exp(interp, env, &exp)) {
                // uh oh, we need to throw an exception and unwind
                frame.exception = @mut option::Some(@mut types::String(@"pattern match refuted"));
            }
        },
        ast::ExpressionStatement(exp) => { eval_exp(interp, env, &exp); }
    };
}

pub fn run_file(interp: @mut Interp, path: &Path) -> () {
    match io::file_reader(path) {
        result::Ok(r) => {
            let tokens = lexer::lex(r);
            let program = parser::parse(tokens);
            run(interp, program);
        },
        result::Err(f) => fail!(f)
    }
}

pub fn run(interp: @mut Interp, prog: ast::Program) -> () {
    for prog.imports.each |imp| {
        import_module(interp, &imp.module, imp.qualified);
    }

    for prog.records.each |rec| {
        interp.record_types.insert(@rec.name, @*rec);
    }

    for prog.body.each |stmt| {
        let mut frame = wind(interp, interp.root, stmt.lineno);

        exec_stmt(interp, interp.root, stmt);

        match *frame.exception {
            option::None => unwind(interp),
            option::Some(f) => {
                fail!(fmt!("%? in %s:%u", f, frame.file, frame.lineno))
            }
        }
    }
}
<|endoftext|>


<commit_before>#![feature(box_syntax)]
// This disables the test completely:
// ignore-stage1

fn main() {
    // With the nested Vec, this is calling Offset(Unique::empty(), 0).
    let args : Vec<Vec<i32>> = Vec::new();
    let local = box args;
}
<commit_msg>enable a test that was accidentally left disabled<commit_after>#![feature(box_syntax)]

fn main() {
    // With the nested Vec, this is calling Offset(Unique::empty(), 0) on drop.
    let args : Vec<Vec<i32>> = Vec::new();
    let _ = box args;
}
<|endoftext|>


<commit_before>use crate::discord_client::{CommandHandler, DiscordClient};
use serenity::{
    client,
    model::{channel::Message, gateway::Ready},
    prelude::*,
};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::thread;

type BoxedThreadsafeCommandHandler = Box<Arc<Mutex<CommandHandler + Send>>>;

pub struct SerenityDiscordClient {
    serenity_client: Arc<Mutex<Client>>,
    command_callbacks: Arc<Mutex<HashMap<String, BoxedThreadsafeCommandHandler>>>,
}

struct SerenityDiscordHandler {
    command_callbacks: Arc<Mutex<HashMap<String, BoxedThreadsafeCommandHandler>>>,
    command_prefix: &'static str,
}

impl DiscordClient for SerenityDiscordClient {
    fn new(token: &str) -> Self {
        println!("valid token: {}", client::validate_token(token).is_ok());

        let command_callbacks = Arc::new(Mutex::new(HashMap::new()));
        let serenity_handler = SerenityDiscordHandler {
            command_callbacks: command_callbacks.clone(),
            command_prefix: "!", //TODO: make this configurable in case of clashes with other bots
        };

        let serenity_client = Arc::new(Mutex::new(
            Client::new(token, serenity_handler).expect("Error creating serenity client"),
        ));
        println!("created client");

        let thread_serenity_client = serenity_client.clone();
        thread::spawn(move || {
            if let Err(why) = thread_serenity_client.lock().unwrap().start() {
                println!("An error occurred while running the client: {:?}", why);
            }
        });
        println!("started connection");

        SerenityDiscordClient {
            serenity_client: serenity_client.clone(),
            command_callbacks: command_callbacks.clone(),
        }
    }

    fn register_command<T>(
        &self,
        command: &str,
        command_handler: Arc<Mutex<T>>,
    ) -> Result<(), Box<std::error::Error>>
    where
        T: CommandHandler + Send + 'static,
    {
        if self
            .command_callbacks
            .lock()
            .unwrap()
            .insert(command.to_string(), Box::new(command_handler))
            .is_some()
        {
            panic!("command was entered twice for {}", command);
        }
        Ok(())
    }
}

impl SerenityDiscordHandler {
    fn get_command_name(&self, full_command: &str) -> Option<String> {
        let mut result: Option<String> = None;

        if full_command.starts_with(self.command_prefix) {
            if let Some(command_with_prefix) = full_command.split_whitespace().nth(0) {
                if let Some(command) = command_with_prefix
                    .chars()
                    .next()
                    .map(|c| &command_with_prefix[c.len_utf8()..])
                {
                    result = Some(command.to_string());
                }
            }
        }

        result
    }
}

impl EventHandler for SerenityDiscordHandler {
    fn message(&self, ctx: Context, msg: Message) {
        if let Some(command) = self.get_command_name(&msg.content) {
            if let Some(command_handler) = self.command_callbacks.lock().unwrap().get(&command) {
                if let Err(err) = command_handler.lock().unwrap().process_command(
                    &msg.content,
                    msg.guild_id.unwrap().0,
                    &|output| {
                        if let Err(err) = msg.channel_id.say(&ctx.http, output) {
                            println!("Error sending message: {:?}", err);
                        }
                    },
                ) {
                    println!("Error processing command: {:?}", err);
                }
            };
        };
    }

    fn ready(&self, _: Context, ready: Ready) {
        println!("{} is connected!", ready.user.name);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_command_name() {
        let command_callbacks = Arc::new(Mutex::new(HashMap::new()));

        let handler = SerenityDiscordHandler {
            command_callbacks: command_callbacks.clone(),
            command_prefix: "!",
        };

        assert_eq!(
            Some(String::from("ping")),
            handler.get_command_name("!ping")
        );
        assert_eq!(Some(String::from("pic")), handler.get_command_name("!pic"));
        assert_eq!(
            Some(String::from("pic")),
            handler.get_command_name("!pic tag1 tag2 tag3")
        );
        assert_eq!(
            Some(String::from("pic")),
            handler.get_command_name("!pic ももよ まじこい")
        );
        assert_eq!(
            Some(String::from("辞書")),
            handler.get_command_name("!辞書")
        );
        assert_eq!(
            Some(String::from("辞書")),
            handler.get_command_name("!辞書 勉強")
        );
    }
}
<commit_msg>fix clippy lint for unused variable<commit_after>use crate::discord_client::{CommandHandler, DiscordClient};
use serenity::{
    client,
    model::{channel::Message, gateway::Ready},
    prelude::*,
};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::thread;

type BoxedThreadsafeCommandHandler = Box<Arc<Mutex<CommandHandler + Send>>>;

pub struct SerenityDiscordClient {
    command_callbacks: Arc<Mutex<HashMap<String, BoxedThreadsafeCommandHandler>>>,
}

struct SerenityDiscordHandler {
    command_callbacks: Arc<Mutex<HashMap<String, BoxedThreadsafeCommandHandler>>>,
    command_prefix: &'static str,
}

impl DiscordClient for SerenityDiscordClient {
    fn new(token: &str) -> Self {
        println!("valid token: {}", client::validate_token(token).is_ok());

        let command_callbacks = Arc::new(Mutex::new(HashMap::new()));
        let serenity_handler = SerenityDiscordHandler {
            command_callbacks: command_callbacks.clone(),
            command_prefix: "!", //TODO: make this configurable in case of clashes with other bots
        };

        let serenity_client = Arc::new(Mutex::new(
            Client::new(token, serenity_handler).expect("Error creating serenity client"),
        ));
        println!("created client");

        let thread_serenity_client = serenity_client.clone();
        thread::spawn(move || {
            if let Err(why) = thread_serenity_client.lock().unwrap().start() {
                println!("An error occurred while running the client: {:?}", why);
            }
        });
        println!("started connection");

        SerenityDiscordClient {
            command_callbacks: command_callbacks.clone(),
        }
    }

    fn register_command<T>(
        &self,
        command: &str,
        command_handler: Arc<Mutex<T>>,
    ) -> Result<(), Box<std::error::Error>>
    where
        T: CommandHandler + Send + 'static,
    {
        if self
            .command_callbacks
            .lock()
            .unwrap()
            .insert(command.to_string(), Box::new(command_handler))
            .is_some()
        {
            panic!("command was entered twice for {}", command);
        }
        Ok(())
    }
}

impl SerenityDiscordHandler {
    fn get_command_name(&self, full_command: &str) -> Option<String> {
        let mut result: Option<String> = None;

        if full_command.starts_with(self.command_prefix) {
            if let Some(command_with_prefix) = full_command.split_whitespace().nth(0) {
                if let Some(command) = command_with_prefix
                    .chars()
                    .next()
                    .map(|c| &command_with_prefix[c.len_utf8()..])
                {
                    result = Some(command.to_string());
                }
            }
        }

        result
    }
}

impl EventHandler for SerenityDiscordHandler {
    fn message(&self, ctx: Context, msg: Message) {
        if let Some(command) = self.get_command_name(&msg.content) {
            if let Some(command_handler) = self.command_callbacks.lock().unwrap().get(&command) {
                if let Err(err) = command_handler.lock().unwrap().process_command(
                    &msg.content,
                    msg.guild_id.unwrap().0,
                    &|output| {
                        if let Err(err) = msg.channel_id.say(&ctx.http, output) {
                            println!("Error sending message: {:?}", err);
                        }
                    },
                ) {
                    println!("Error processing command: {:?}", err);
                }
            };
        };
    }

    fn ready(&self, _: Context, ready: Ready) {
        println!("{} is connected!", ready.user.name);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_command_name() {
        let command_callbacks = Arc::new(Mutex::new(HashMap::new()));

        let handler = SerenityDiscordHandler {
            command_callbacks: command_callbacks.clone(),
            command_prefix: "!",
        };

        assert_eq!(
            Some(String::from("ping")),
            handler.get_command_name("!ping")
        );
        assert_eq!(Some(String::from("pic")), handler.get_command_name("!pic"));
        assert_eq!(
            Some(String::from("pic")),
            handler.get_command_name("!pic tag1 tag2 tag3")
        );
        assert_eq!(
            Some(String::from("pic")),
            handler.get_command_name("!pic ももよ まじこい")
        );
        assert_eq!(
            Some(String::from("辞書")),
            handler.get_command_name("!辞書")
        );
        assert_eq!(
            Some(String::from("辞書")),
            handler.get_command_name("!辞書 勉強")
        );
    }
}
<|endoftext|>


<commit_before>macro_rules! implement {
    {
        impl $type:ident {
            int_type = $int_type:ident,
            floor = $floor_intrinsic:literal,
            ceil = $ceil_intrinsic:literal,
            round = $round_intrinsic:literal,
            trunc = $trunc_intrinsic:literal,
        }
    } => {
        mod $type {
            #[allow(improper_ctypes)]
            extern "C" {
                #[link_name = $floor_intrinsic]
                fn floor_intrinsic(x: crate::$type) -> crate::$type;
                #[link_name = $ceil_intrinsic]
                fn ceil_intrinsic(x: crate::$type) -> crate::$type;
                #[link_name = $round_intrinsic]
                fn round_intrinsic(x: crate::$type) -> crate::$type;
                #[link_name = $trunc_intrinsic]
                fn trunc_intrinsic(x: crate::$type) -> crate::$type;
            }

            impl crate::$type {
                /// Returns the largest integer less than or equal to each lane.
                #[must_use = "method returns a new vector and does not mutate the original value"]
                #[inline]
                pub fn floor(self) -> Self {
                    unsafe { floor_intrinsic(self) }
                }

                /// Returns the smallest integer greater than or equal to each lane.
                #[must_use = "method returns a new vector and does not mutate the original value"]
                #[inline]
                pub fn ceil(self) -> Self {
                    unsafe { ceil_intrinsic(self) }
                }

                /// Returns the nearest integer to each lane. Round half-way cases away from 0.0.
                #[must_use = "method returns a new vector and does not mutate the original value"]
                #[inline]
                pub fn round(self) -> Self {
                    unsafe { round_intrinsic(self) }
                }

                /// Returns the integer part of each lane.
                #[must_use = "method returns a new vector and does not mutate the original value"]
                #[inline]
                pub fn trunc(self) -> Self {
                    unsafe { trunc_intrinsic(self) }
                }

                /// Returns the fractional part of each lane.
                #[must_use = "method returns a new vector and does not mutate the original value"]
                #[inline]
                pub fn fract(self) -> Self {
                    self - self.trunc()
                }

                /// Rounds toward zero and converts to the same-width integer type, assuming that
                /// the value is finite and fits in that type.
                ///
                /// # Safety
                /// The value must:
                ///
                /// * Not be NaN
                /// * Not be infinite
                /// * Be representable in the return type, after truncating off its fractional part
                #[inline]
                pub unsafe fn to_int_unchecked(self) -> crate::$int_type {
                    crate::intrinsics::simd_cast(self)
                }

                /// Returns the nearest integer to each lane. Round half-way cases away from 0.0.
                #[inline]
                pub fn round_from_int(value: crate::$int_type) -> Self {
                    unsafe { crate::intrinsics::simd_cast(value) }
                }
            }
        }
    }
}

implement! {
    impl f32x2 {
        int_type = i32x2,
        floor = "llvm.floor.v2f32",
        ceil = "llvm.ceil.v2f32",
        round = "llvm.round.v2f32",
        trunc = "llvm.trunc.v2f32",
    }
}

implement! {
    impl f32x4 {
        int_type = i32x4,
        floor = "llvm.floor.v4f32",
        ceil = "llvm.ceil.v4f32",
        round = "llvm.round.v4f32",
        trunc = "llvm.trunc.v4f32",
    }
}

implement! {
    impl f32x8 {
        int_type = i32x8,
        floor = "llvm.floor.v8f32",
        ceil = "llvm.ceil.v8f32",
        round = "llvm.round.v8f32",
        trunc = "llvm.trunc.v8f32",
    }
}

implement! {
    impl f32x16 {
        int_type = i32x16,
        floor = "llvm.floor.v16f32",
        ceil = "llvm.ceil.v16f32",
        round = "llvm.round.v16f32",
        trunc = "llvm.trunc.v16f32",
    }
}

implement! {
    impl f64x2 {
        int_type = i64x2,
        floor = "llvm.floor.v2f64",
        ceil = "llvm.ceil.v2f64",
        round = "llvm.round.v2f64",
        trunc = "llvm.trunc.v2f64",
    }
}

implement! {
    impl f64x4 {
        int_type = i64x4,
        floor = "llvm.floor.v4f64",
        ceil = "llvm.ceil.v4f64",
        round = "llvm.round.v4f64",
        trunc = "llvm.trunc.v4f64",
    }
}

implement! {
    impl f64x8 {
        int_type = i64x8,
        floor = "llvm.floor.v8f64",
        ceil = "llvm.ceil.v8f64",
        round = "llvm.round.v8f64",
        trunc = "llvm.trunc.v8f64",
    }
}
<commit_msg>Fix comment<commit_after>macro_rules! implement {
    {
        impl $type:ident {
            int_type = $int_type:ident,
            floor = $floor_intrinsic:literal,
            ceil = $ceil_intrinsic:literal,
            round = $round_intrinsic:literal,
            trunc = $trunc_intrinsic:literal,
        }
    } => {
        mod $type {
            #[allow(improper_ctypes)]
            extern "C" {
                #[link_name = $floor_intrinsic]
                fn floor_intrinsic(x: crate::$type) -> crate::$type;
                #[link_name = $ceil_intrinsic]
                fn ceil_intrinsic(x: crate::$type) -> crate::$type;
                #[link_name = $round_intrinsic]
                fn round_intrinsic(x: crate::$type) -> crate::$type;
                #[link_name = $trunc_intrinsic]
                fn trunc_intrinsic(x: crate::$type) -> crate::$type;
            }

            impl crate::$type {
                /// Returns the largest integer less than or equal to each lane.
                #[must_use = "method returns a new vector and does not mutate the original value"]
                #[inline]
                pub fn floor(self) -> Self {
                    unsafe { floor_intrinsic(self) }
                }

                /// Returns the smallest integer greater than or equal to each lane.
                #[must_use = "method returns a new vector and does not mutate the original value"]
                #[inline]
                pub fn ceil(self) -> Self {
                    unsafe { ceil_intrinsic(self) }
                }

                /// Returns the nearest integer to each lane. Round half-way cases away from 0.0.
                #[must_use = "method returns a new vector and does not mutate the original value"]
                #[inline]
                pub fn round(self) -> Self {
                    unsafe { round_intrinsic(self) }
                }

                /// Returns the integer part of each lane.
                #[must_use = "method returns a new vector and does not mutate the original value"]
                #[inline]
                pub fn trunc(self) -> Self {
                    unsafe { trunc_intrinsic(self) }
                }

                /// Returns the fractional part of each lane.
                #[must_use = "method returns a new vector and does not mutate the original value"]
                #[inline]
                pub fn fract(self) -> Self {
                    self - self.trunc()
                }

                /// Rounds toward zero and converts to the same-width integer type, assuming that
                /// the value is finite and fits in that type.
                ///
                /// # Safety
                /// The value must:
                ///
                /// * Not be NaN
                /// * Not be infinite
                /// * Be representable in the return type, after truncating off its fractional part
                #[inline]
                pub unsafe fn to_int_unchecked(self) -> crate::$int_type {
                    crate::intrinsics::simd_cast(self)
                }

                /// Creates a floating-point vector from an integer vector.  Rounds values that are
                /// not exactly representable.
                #[inline]
                pub fn round_from_int(value: crate::$int_type) -> Self {
                    unsafe { crate::intrinsics::simd_cast(value) }
                }
            }
        }
    }
}

implement! {
    impl f32x2 {
        int_type = i32x2,
        floor = "llvm.floor.v2f32",
        ceil = "llvm.ceil.v2f32",
        round = "llvm.round.v2f32",
        trunc = "llvm.trunc.v2f32",
    }
}

implement! {
    impl f32x4 {
        int_type = i32x4,
        floor = "llvm.floor.v4f32",
        ceil = "llvm.ceil.v4f32",
        round = "llvm.round.v4f32",
        trunc = "llvm.trunc.v4f32",
    }
}

implement! {
    impl f32x8 {
        int_type = i32x8,
        floor = "llvm.floor.v8f32",
        ceil = "llvm.ceil.v8f32",
        round = "llvm.round.v8f32",
        trunc = "llvm.trunc.v8f32",
    }
}

implement! {
    impl f32x16 {
        int_type = i32x16,
        floor = "llvm.floor.v16f32",
        ceil = "llvm.ceil.v16f32",
        round = "llvm.round.v16f32",
        trunc = "llvm.trunc.v16f32",
    }
}

implement! {
    impl f64x2 {
        int_type = i64x2,
        floor = "llvm.floor.v2f64",
        ceil = "llvm.ceil.v2f64",
        round = "llvm.round.v2f64",
        trunc = "llvm.trunc.v2f64",
    }
}

implement! {
    impl f64x4 {
        int_type = i64x4,
        floor = "llvm.floor.v4f64",
        ceil = "llvm.ceil.v4f64",
        round = "llvm.round.v4f64",
        trunc = "llvm.trunc.v4f64",
    }
}

implement! {
    impl f64x8 {
        int_type = i64x8,
        floor = "llvm.floor.v8f64",
        ceil = "llvm.ceil.v8f64",
        round = "llvm.round.v8f64",
        trunc = "llvm.trunc.v8f64",
    }
}
<|endoftext|>


<commit_before>#[macro_use]
extern crate failure;
extern crate parking_lot;
#[macro_use]
extern crate log;
extern crate once_cell;
extern crate libsyntax2;
extern crate libeditor;
extern crate fst;
extern crate rayon;
extern crate relative_path;

mod symbol_index;
mod module_map;
mod api;

use std::{
    fmt,
    panic,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering::SeqCst},
    },
    collections::hash_map::HashMap,
    time::Instant,
};

use relative_path::{RelativePath,RelativePathBuf};
use once_cell::sync::OnceCell;
use rayon::prelude::*;

use libsyntax2::{
    File,
    TextUnit, TextRange, SmolStr,
    ast::{self, AstNode, NameOwner},
    SyntaxKind::*,
};
use libeditor::{LineIndex, FileSymbol, find_node_at_offset};

use self::{
    symbol_index::FileSymbols,
    module_map::{ModuleMap, ChangeKind, Problem},
};
pub use self::symbol_index::Query;
pub use self::api::*;

pub type Result<T> = ::std::result::Result<T, ::failure::Error>;

pub trait FileResolver: Send + Sync + 'static {
    fn file_stem(&self, id: FileId) -> String;
    fn resolve(&self, id: FileId, path: &RelativePath) -> Option<FileId>;
}

#[derive(Debug)]
pub struct WorldState {
    data: Arc<WorldData>
}

pub struct World {
    needs_reindex: AtomicBool,
    file_resolver: Arc<FileResolver>,
    data: Arc<WorldData>,
}

impl fmt::Debug for World {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        (&*self.data).fmt(f)
    }
}

impl Clone for World {
    fn clone(&self) -> World {
        World {
            needs_reindex: AtomicBool::new(self.needs_reindex.load(SeqCst)),
            file_resolver: Arc::clone(&self.file_resolver),
            data: Arc::clone(&self.data),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FileId(pub u32);

impl WorldState {
    pub fn new() -> WorldState {
        WorldState {
            data: Arc::new(WorldData::default()),
        }
    }

    pub fn snapshot(
        &self,
        file_resolver: impl FileResolver,
    ) -> World {
        World {
            needs_reindex: AtomicBool::new(false),
            file_resolver: Arc::new(file_resolver),
            data: self.data.clone()
        }
    }

    pub fn analysis(
        &self,
        file_resolver: impl FileResolver,
    ) -> Analysis {
        Analysis { imp: self.snapshot(file_resolver) }
    }

    pub fn change_file(&mut self, file_id: FileId, text: Option<String>) {
        self.change_files(::std::iter::once((file_id, text)));
    }

    pub fn change_files(&mut self, changes: impl Iterator<Item=(FileId, Option<String>)>) {
        let data = self.data_mut();
        for (file_id, text) in changes {
            let change_kind = if data.file_map.remove(&file_id).is_some() {
                if text.is_some() {
                    ChangeKind::Update
                } else {
                    ChangeKind::Delete
                }
            } else {
                ChangeKind::Insert
            };
            data.module_map.update_file(file_id, change_kind);
            data.file_map.remove(&file_id);
            if let Some(text) = text {
                let file_data = FileData::new(text);
                data.file_map.insert(file_id, Arc::new(file_data));
            } else {
                data.file_map.remove(&file_id);
            }
        }
    }

    fn data_mut(&mut self) -> &mut WorldData {
        if Arc::get_mut(&mut self.data).is_none() {
            self.data = Arc::new(WorldData {
                file_map: self.data.file_map.clone(),
                module_map: self.data.module_map.clone(),
            });
        }
        Arc::get_mut(&mut self.data).unwrap()
    }
}

#[derive(Debug)]
pub struct QuickFix {
    pub fs_ops: Vec<FsOp>,
}

#[derive(Debug)]
pub enum FsOp {
    CreateFile {
        anchor: FileId,
        path: RelativePathBuf,
    },
    MoveFile {
        file: FileId,
        path: RelativePathBuf,
    }
}

impl World {
    pub fn file_syntax(&self, file_id: FileId) -> Result<File> {
        let data = self.file_data(file_id)?;
        Ok(data.syntax().clone())
    }

    pub fn file_line_index(&self, id: FileId) -> Result<LineIndex> {
        let data = self.file_data(id)?;
        let index = data.lines
            .get_or_init(|| LineIndex::new(&data.text));
        Ok(index.clone())
    }

    pub fn world_symbols(&self, mut query: Query) -> Vec<(FileId, FileSymbol)> {
        self.reindex();
        self.data.file_map.iter()
            .flat_map(move |(id, data)| {
                let symbols = data.symbols();
                query.process(symbols).into_iter().map(move |s| (*id, s))
            })
            .collect()
    }

    pub fn parent_module(&self, id: FileId) -> Vec<(FileId, FileSymbol)> {
        let module_map = &self.data.module_map;
        let id = module_map.file2module(id);
        module_map
            .parent_modules(
                id,
                &*self.file_resolver,
                &|file_id| self.file_syntax(file_id).unwrap(),
            )
            .into_iter()
            .map(|(id, name, node)| {
                let id = module_map.module2file(id);
                let sym = FileSymbol {
                    name,
                    node_range: node.range(),
                    kind: MODULE,
                };
                (id, sym)
            })
            .collect()
    }

    pub fn approximately_resolve_symbol(
        &self,
        id: FileId,
        offset: TextUnit,
    ) -> Result<Vec<(FileId, FileSymbol)>> {
        let file = self.file_syntax(id)?;
        let syntax = file.syntax();
        if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) {
            return Ok(self.index_resolve(name_ref));
        }
        if let Some(name) = find_node_at_offset::<ast::Name>(syntax, offset) {
            if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
                if module.has_semi() {
                    let file_ids = self.resolve_module(id, module);

                    let res = file_ids.into_iter().map(|id| {
                        let name = module.name()
                            .map(|n| n.text())
                            .unwrap_or_else(|| SmolStr::new(""));
                        let symbol = FileSymbol {
                            name,
                            node_range: TextRange::offset_len(0.into(), 0.into()),
                            kind: MODULE,
                        };
                        (id, symbol)
                    }).collect();

                    return Ok(res);
                }
            }
        }
        Ok(vec![])
    }

    pub fn diagnostics(&self, file_id: FileId) -> Vec<Diagnostic> {
        let syntax = self.file_syntax(file_id).unwrap();
        let mut res = libeditor::diagnostics(&syntax)
            .into_iter()
            .map(|d| Diagnostic { range: d.range, message: d.msg, fix: None })
            .collect::<Vec<_>>();

        self.data.module_map.problems(
            file_id,
            &*self.file_resolver,
            &|file_id| self.file_syntax(file_id).unwrap(),
            |name_node, problem| {
                let diag = match problem {
                    Problem::UnresolvedModule { candidate } => {
                        let create_file = FileSystemEdit::CreateFile {
                            anchor: file_id,
                            path: candidate.clone(),
                        };
                        let fix = SourceChange {
                            label: "create module".to_string(),
                            source_file_edits: Vec::new(),
                            file_system_edits: vec![create_file],
                            cursor_position: None,
                        };
                        Diagnostic {
                            range: name_node.syntax().range(),
                            message: "unresolved module".to_string(),
                            fix: Some(fix),
                        }
                    }
                    Problem::NotDirOwner { move_to, candidate } => {
                        let move_file = FileSystemEdit::MoveFile { file: file_id, path: move_to.clone() };
                        let create_file = FileSystemEdit::CreateFile { anchor: file_id, path: move_to.join(candidate) };
                        let fix = SourceChange {
                            label: "move file and create module".to_string(),
                            source_file_edits: Vec::new(),
                            file_system_edits: vec![move_file, create_file],
                            cursor_position: None,
                        };
                        Diagnostic {
                            range: name_node.syntax().range(),
                            message: "can't declare module at this location".to_string(),
                            fix: Some(fix),
                        }
                    }
                };
                res.push(diag)
            }
        );
        res
    }

    pub fn assists(&self, file_id: FileId, offset: TextUnit) -> Vec<SourceChange> {
        let file = self.file_syntax(file_id).unwrap();
        let actions = vec![
            ("flip comma", libeditor::flip_comma(&file, offset).map(|f| f())),
            ("add `#[derive]`", libeditor::add_derive(&file, offset).map(|f| f())),
            ("add impl", libeditor::add_impl(&file, offset).map(|f| f())),
        ];
        let mut res = Vec::new();
        for (name, local_edit) in actions {
            if let Some(local_edit) = local_edit {
                res.push(SourceChange::from_local_edit(
                    file_id, name, local_edit
                ))
            }
        }
        res
    }

    fn index_resolve(&self, name_ref: ast::NameRef) -> Vec<(FileId, FileSymbol)> {
        let name = name_ref.text();
        let mut query = Query::new(name.to_string());
        query.exact();
        query.limit(4);
        self.world_symbols(query)
    }

    fn resolve_module(&self, id: FileId, module: ast::Module) -> Vec<FileId> {
        let name = match module.name() {
            Some(name) => name.text(),
            None => return Vec::new(),
        };
        let module_map = &self.data.module_map;
        let id = module_map.file2module(id);
        module_map
            .child_module_by_name(
                id, name.as_str(),
                &*self.file_resolver,
                &|file_id| self.file_syntax(file_id).unwrap(),
            )
            .into_iter()
            .map(|id| module_map.module2file(id))
            .collect()
    }

    fn reindex(&self) {
        if self.needs_reindex.compare_and_swap(false, true, SeqCst) {
            let now = Instant::now();
            let data = &*self.data;
            data.file_map
                .par_iter()
                .for_each(|(_, data)| drop(data.symbols()));
            info!("parallel indexing took {:?}", now.elapsed());
        }
    }

    fn file_data(&self, file_id: FileId) -> Result<Arc<FileData>> {
        match self.data.file_map.get(&file_id) {
            Some(data) => Ok(data.clone()),
            None => bail!("unknown file: {:?}", file_id),
        }
    }
}

#[derive(Default, Debug)]
struct WorldData {
    file_map: HashMap<FileId, Arc<FileData>>,
    module_map: ModuleMap,
}

#[derive(Debug)]
struct FileData {
    text: String,
    symbols: OnceCell<FileSymbols>,
    syntax: OnceCell<File>,
    lines: OnceCell<LineIndex>,
}

impl FileData {
    fn new(text: String) -> FileData {
        FileData {
            text,
            symbols: OnceCell::new(),
            syntax: OnceCell::new(),
            lines: OnceCell::new(),
        }
    }

    fn syntax(&self) -> &File {
        let text = &self.text;
        let syntax = &self.syntax;
        match panic::catch_unwind(panic::AssertUnwindSafe(|| syntax.get_or_init(|| File::parse(text)))) {
            Ok(file) => file,
            Err(err) => {
                error!("Parser paniced on:\n------\n{}\n------\n", &self.text);
                panic::resume_unwind(err)
            }
        }
    }

    fn syntax_transient(&self) -> File {
        self.syntax.get().map(|s| s.clone())
            .unwrap_or_else(|| File::parse(&self.text))
    }

    fn symbols(&self) -> &FileSymbols {
        let syntax = self.syntax_transient();
        self.symbols
            .get_or_init(|| FileSymbols::new(&syntax))
    }
}
<commit_msg>Make world private<commit_after>#[macro_use]
extern crate failure;
extern crate parking_lot;
#[macro_use]
extern crate log;
extern crate once_cell;
extern crate libsyntax2;
extern crate libeditor;
extern crate fst;
extern crate rayon;
extern crate relative_path;

mod symbol_index;
mod module_map;
mod api;

use std::{
    fmt,
    panic,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering::SeqCst},
    },
    collections::hash_map::HashMap,
    time::Instant,
};

use relative_path::{RelativePath,RelativePathBuf};
use once_cell::sync::OnceCell;
use rayon::prelude::*;

use libsyntax2::{
    File,
    TextUnit, TextRange, SmolStr,
    ast::{self, AstNode, NameOwner},
    SyntaxKind::*,
};
use libeditor::{LineIndex, FileSymbol, find_node_at_offset};

use self::{
    symbol_index::FileSymbols,
    module_map::{ModuleMap, ChangeKind, Problem},
};
pub use self::symbol_index::Query;
pub use self::api::*;

pub type Result<T> = ::std::result::Result<T, ::failure::Error>;

pub trait FileResolver: Send + Sync + 'static {
    fn file_stem(&self, id: FileId) -> String;
    fn resolve(&self, id: FileId, path: &RelativePath) -> Option<FileId>;
}

#[derive(Debug)]
pub struct WorldState {
    data: Arc<WorldData>
}

pub(crate) struct World {
    needs_reindex: AtomicBool,
    file_resolver: Arc<FileResolver>,
    data: Arc<WorldData>,
}

impl fmt::Debug for World {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        (&*self.data).fmt(f)
    }
}

impl Clone for World {
    fn clone(&self) -> World {
        World {
            needs_reindex: AtomicBool::new(self.needs_reindex.load(SeqCst)),
            file_resolver: Arc::clone(&self.file_resolver),
            data: Arc::clone(&self.data),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FileId(pub u32);

impl WorldState {
    pub fn new() -> WorldState {
        WorldState {
            data: Arc::new(WorldData::default()),
        }
    }

    pub fn analysis(
        &self,
        file_resolver: impl FileResolver,
    ) -> Analysis {
        let imp = World {
            needs_reindex: AtomicBool::new(false),
            file_resolver: Arc::new(file_resolver),
            data: self.data.clone()
        };
        Analysis { imp }
    }

    pub fn change_file(&mut self, file_id: FileId, text: Option<String>) {
        self.change_files(::std::iter::once((file_id, text)));
    }

    pub fn change_files(&mut self, changes: impl Iterator<Item=(FileId, Option<String>)>) {
        let data = self.data_mut();
        for (file_id, text) in changes {
            let change_kind = if data.file_map.remove(&file_id).is_some() {
                if text.is_some() {
                    ChangeKind::Update
                } else {
                    ChangeKind::Delete
                }
            } else {
                ChangeKind::Insert
            };
            data.module_map.update_file(file_id, change_kind);
            data.file_map.remove(&file_id);
            if let Some(text) = text {
                let file_data = FileData::new(text);
                data.file_map.insert(file_id, Arc::new(file_data));
            } else {
                data.file_map.remove(&file_id);
            }
        }
    }

    fn data_mut(&mut self) -> &mut WorldData {
        if Arc::get_mut(&mut self.data).is_none() {
            self.data = Arc::new(WorldData {
                file_map: self.data.file_map.clone(),
                module_map: self.data.module_map.clone(),
            });
        }
        Arc::get_mut(&mut self.data).unwrap()
    }
}

#[derive(Debug)]
pub struct QuickFix {
    pub fs_ops: Vec<FsOp>,
}

#[derive(Debug)]
pub enum FsOp {
    CreateFile {
        anchor: FileId,
        path: RelativePathBuf,
    },
    MoveFile {
        file: FileId,
        path: RelativePathBuf,
    }
}

impl World {
    pub fn file_syntax(&self, file_id: FileId) -> Result<File> {
        let data = self.file_data(file_id)?;
        Ok(data.syntax().clone())
    }

    pub fn file_line_index(&self, id: FileId) -> Result<LineIndex> {
        let data = self.file_data(id)?;
        let index = data.lines
            .get_or_init(|| LineIndex::new(&data.text));
        Ok(index.clone())
    }

    pub fn world_symbols(&self, mut query: Query) -> Vec<(FileId, FileSymbol)> {
        self.reindex();
        self.data.file_map.iter()
            .flat_map(move |(id, data)| {
                let symbols = data.symbols();
                query.process(symbols).into_iter().map(move |s| (*id, s))
            })
            .collect()
    }

    pub fn parent_module(&self, id: FileId) -> Vec<(FileId, FileSymbol)> {
        let module_map = &self.data.module_map;
        let id = module_map.file2module(id);
        module_map
            .parent_modules(
                id,
                &*self.file_resolver,
                &|file_id| self.file_syntax(file_id).unwrap(),
            )
            .into_iter()
            .map(|(id, name, node)| {
                let id = module_map.module2file(id);
                let sym = FileSymbol {
                    name,
                    node_range: node.range(),
                    kind: MODULE,
                };
                (id, sym)
            })
            .collect()
    }

    pub fn approximately_resolve_symbol(
        &self,
        id: FileId,
        offset: TextUnit,
    ) -> Result<Vec<(FileId, FileSymbol)>> {
        let file = self.file_syntax(id)?;
        let syntax = file.syntax();
        if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) {
            return Ok(self.index_resolve(name_ref));
        }
        if let Some(name) = find_node_at_offset::<ast::Name>(syntax, offset) {
            if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
                if module.has_semi() {
                    let file_ids = self.resolve_module(id, module);

                    let res = file_ids.into_iter().map(|id| {
                        let name = module.name()
                            .map(|n| n.text())
                            .unwrap_or_else(|| SmolStr::new(""));
                        let symbol = FileSymbol {
                            name,
                            node_range: TextRange::offset_len(0.into(), 0.into()),
                            kind: MODULE,
                        };
                        (id, symbol)
                    }).collect();

                    return Ok(res);
                }
            }
        }
        Ok(vec![])
    }

    pub fn diagnostics(&self, file_id: FileId) -> Vec<Diagnostic> {
        let syntax = self.file_syntax(file_id).unwrap();
        let mut res = libeditor::diagnostics(&syntax)
            .into_iter()
            .map(|d| Diagnostic { range: d.range, message: d.msg, fix: None })
            .collect::<Vec<_>>();

        self.data.module_map.problems(
            file_id,
            &*self.file_resolver,
            &|file_id| self.file_syntax(file_id).unwrap(),
            |name_node, problem| {
                let diag = match problem {
                    Problem::UnresolvedModule { candidate } => {
                        let create_file = FileSystemEdit::CreateFile {
                            anchor: file_id,
                            path: candidate.clone(),
                        };
                        let fix = SourceChange {
                            label: "create module".to_string(),
                            source_file_edits: Vec::new(),
                            file_system_edits: vec![create_file],
                            cursor_position: None,
                        };
                        Diagnostic {
                            range: name_node.syntax().range(),
                            message: "unresolved module".to_string(),
                            fix: Some(fix),
                        }
                    }
                    Problem::NotDirOwner { move_to, candidate } => {
                        let move_file = FileSystemEdit::MoveFile { file: file_id, path: move_to.clone() };
                        let create_file = FileSystemEdit::CreateFile { anchor: file_id, path: move_to.join(candidate) };
                        let fix = SourceChange {
                            label: "move file and create module".to_string(),
                            source_file_edits: Vec::new(),
                            file_system_edits: vec![move_file, create_file],
                            cursor_position: None,
                        };
                        Diagnostic {
                            range: name_node.syntax().range(),
                            message: "can't declare module at this location".to_string(),
                            fix: Some(fix),
                        }
                    }
                };
                res.push(diag)
            }
        );
        res
    }

    pub fn assists(&self, file_id: FileId, offset: TextUnit) -> Vec<SourceChange> {
        let file = self.file_syntax(file_id).unwrap();
        let actions = vec![
            ("flip comma", libeditor::flip_comma(&file, offset).map(|f| f())),
            ("add `#[derive]`", libeditor::add_derive(&file, offset).map(|f| f())),
            ("add impl", libeditor::add_impl(&file, offset).map(|f| f())),
        ];
        let mut res = Vec::new();
        for (name, local_edit) in actions {
            if let Some(local_edit) = local_edit {
                res.push(SourceChange::from_local_edit(
                    file_id, name, local_edit
                ))
            }
        }
        res
    }

    fn index_resolve(&self, name_ref: ast::NameRef) -> Vec<(FileId, FileSymbol)> {
        let name = name_ref.text();
        let mut query = Query::new(name.to_string());
        query.exact();
        query.limit(4);
        self.world_symbols(query)
    }

    fn resolve_module(&self, id: FileId, module: ast::Module) -> Vec<FileId> {
        let name = match module.name() {
            Some(name) => name.text(),
            None => return Vec::new(),
        };
        let module_map = &self.data.module_map;
        let id = module_map.file2module(id);
        module_map
            .child_module_by_name(
                id, name.as_str(),
                &*self.file_resolver,
                &|file_id| self.file_syntax(file_id).unwrap(),
            )
            .into_iter()
            .map(|id| module_map.module2file(id))
            .collect()
    }

    fn reindex(&self) {
        if self.needs_reindex.compare_and_swap(false, true, SeqCst) {
            let now = Instant::now();
            let data = &*self.data;
            data.file_map
                .par_iter()
                .for_each(|(_, data)| drop(data.symbols()));
            info!("parallel indexing took {:?}", now.elapsed());
        }
    }

    fn file_data(&self, file_id: FileId) -> Result<Arc<FileData>> {
        match self.data.file_map.get(&file_id) {
            Some(data) => Ok(data.clone()),
            None => bail!("unknown file: {:?}", file_id),
        }
    }
}

#[derive(Default, Debug)]
struct WorldData {
    file_map: HashMap<FileId, Arc<FileData>>,
    module_map: ModuleMap,
}

#[derive(Debug)]
struct FileData {
    text: String,
    symbols: OnceCell<FileSymbols>,
    syntax: OnceCell<File>,
    lines: OnceCell<LineIndex>,
}

impl FileData {
    fn new(text: String) -> FileData {
        FileData {
            text,
            symbols: OnceCell::new(),
            syntax: OnceCell::new(),
            lines: OnceCell::new(),
        }
    }

    fn syntax(&self) -> &File {
        let text = &self.text;
        let syntax = &self.syntax;
        match panic::catch_unwind(panic::AssertUnwindSafe(|| syntax.get_or_init(|| File::parse(text)))) {
            Ok(file) => file,
            Err(err) => {
                error!("Parser paniced on:\n------\n{}\n------\n", &self.text);
                panic::resume_unwind(err)
            }
        }
    }

    fn syntax_transient(&self) -> File {
        self.syntax.get().map(|s| s.clone())
            .unwrap_or_else(|| File::parse(&self.text))
    }

    fn symbols(&self) -> &FileSymbols {
        let syntax = self.syntax_transient();
        self.symbols
            .get_or_init(|| FileSymbols::new(&syntax))
    }
}
<|endoftext|>


<commit_before>pub use std::{
    any::{Any, TypeId},
    cell::RefCell,
    collections::{HashMap, HashSet},
    fmt::Debug,
    rc::Rc,
};

pub use crate::*;
pub use api::*;
pub use ecs::*;
pub use orbtk_api::css_engine::{Selector, Theme};
pub use orbtk_render::prelude::Image;
pub use proc_macros::*;
pub use theme::{
    colors,
    default_theme,
    fonts,
    light_theme,
    vector_graphics::{
	material_icons_font_ttf,
	material_icons_font,
	material_icons_round_font,
	material_icons_sharp_font,
	material_icons_twotone_font,
    }
};
pub use utils::*;
<commit_msg>widgets/prelude: update vector_graphics woff2 font<commit_after>pub use std::{
    any::{Any, TypeId},
    cell::RefCell,
    collections::{HashMap, HashSet},
    fmt::Debug,
    rc::Rc,
};

pub use crate::*;
pub use api::*;
pub use ecs::*;
pub use orbtk_api::css_engine::{Selector, Theme};
pub use orbtk_render::prelude::Image;
pub use proc_macros::*;
pub use theme::{
    colors,
    default_theme,
    fonts,
    light_theme,
    vector_graphics::{
        material_icons_font_ttf,
        material_icons_baseline_font,
        material_icons_round_font,
        material_icons_sharp_font,
        material_icons_twotone_font,
    }
};
pub use utils::*;
<|endoftext|>