1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! A column-based in-memory database for [Data-Oriented Design][dod].
//!
//! # Safety
//! This crate *lies about safety*. Rust wants to be monkey-safe; v11 aims for merely derp-safe.
//!
//! The danger zones are easy to avoid:
//!
//! - Calling `register()` functions on domains, properties and tables. This should only be done from the
//! main thread, before any `Universe`s have been created.
//! - Doing strange things with the references in a table lock. (Namely, `mem::swap`.)
//! - Using `pub` items that are marked `#[doc(hidden)]`. These should only be used by
//! macro-generated code.
//!
//!
//! [dod]: http://www.dataorienteddesign.com/dodmain/
// FIXME: Change `References` to [`References`].
// FIXME: Could use some hefty reorganization.

#[allow(unused_imports)]
#[macro_use]
// We don't actually use macros or the derive, but this silences a warning.
extern crate v11_macros;
#[macro_use]
extern crate procedural_masquerade;
pub extern crate serde;
#[macro_use]
extern crate serde_derive;
#[macro_use]
pub extern crate erased_serde; // There are many things that need to be erased.
extern crate itertools;
#[doc(hidden)]
pub extern crate num_traits;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate mopa;

use std::sync::*;


#[macro_use]
pub mod domain;
#[macro_use]
pub mod table_macro;
pub mod tables;
#[macro_use]
pub mod property;
#[doc(hidden)]
pub mod intern;
pub mod columns;
pub mod index;
pub mod map_index;
pub mod storage;
pub mod tracking;
pub mod event;

#[macro_use]
pub mod context;

// Util. Buncha these could become crates!
pub mod joincore;
mod assert_sorted;
pub mod any_slice;
mod serial;

// FIXME: #[cfg(rustdoc)] will be a thing eventually.
#[cfg(feature = "doc")]
pub mod examples;

#[cfg(not(feature = "doc"))]
/// Run `cargo doc --features doc --open` to get documentation on example macro output.
pub mod examples {}

#[cfg(feature = "doc")]
#[doc(hidden)]
pub mod v11 {
    pub use super::*;
    // This is a work around for not having $crate in the procedural_masquerade.
    // It is necessary for the macro invokations in `examples`.
    // It might be possible to emulate $crate...
}


/// Trait describing bounds that all storable types must satisfy.
///
/// Types that implement this trait shouldn't implement `Drop`,
/// and they shouldn't be `mem::needs_drop`.
/// However this can not yet be expressed... and actually isn't even required yet.
///
/// There are additional requirements not expressed by this type.
// FIXME: !Drop
pub trait Storable: 'static + Send + Sync + Sized /* + !Drop */ {
    #[doc(hidden)]
    fn assert_no_drop() {
        // FIXME: Call me, maybe.
        if ::std::mem::needs_drop::<Self>() {
            panic!("Column element needs_drop");
        }
    }
}

impl<T> Storable for T where T: 'static + Send + Sync + Sized /* + !Drop */ {}

pub type GuardedUniverse = Arc<RwLock<Universe>>;

use domain::{DomainName, MaybeDomain};

/**
 * A context object whose reference should be passed around everywhere.
 * */
pub struct Universe {
    #[doc(hidden)] pub domains: Vec<MaybeDomain>,
    pub event_handlers: ::event::EventHandlers,
}

/// Universe manipulation methods.
impl Universe {
    // FIXME: Mark this unsafe?
    pub fn new(domains: &[DomainName]) -> Universe {
        let mut ret = Universe {
            domains: Self::get_domains(domains),
            event_handlers: Default::default(),
        };
        for domain in domains {
            ret.init_domain(*domain);
        }
        ret
    }

    /// Converts to a form shareable with other threads.
    pub fn guard(self) -> GuardedUniverse { Arc::new(RwLock::new(self)) }

    /// Returns a string describing all the tables in the Universe.
    /// (This does not include their contents.)
    pub fn info(&self) -> String {
        let mut out = "".to_owned();
        for domain in &self.domains {
            let domain = match *domain {
                MaybeDomain::Unset(_) => continue,
                MaybeDomain::Domain(ref i) => i,
            };
            use itertools::Itertools;
            let info: String = domain.tables.iter().map(|(_, table)| {
                table.read().unwrap().info()
            }).join(" ");
            out += &format!("{}: {}\n", domain.name, info);
        }
        out
    }
}
use std::fmt;
impl fmt::Debug for Universe {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "Universe:")?;
        for domain in &self.domains {
            writeln!(f, "\t{:?}", domain)?;
        }
        write!(f, "")
    }
}

/// Return value for function parameters passed into `$table.visit`.
#[derive(Debug, Clone)]
pub enum Action<IT> {
    /// If `remove` is true, then this row is removed.
    /// Any items yielded by `add` are inserted prior to the next item.
    /// They will not be visited.
    Continue {
        remove: bool,
        add: IT,
    },
    /// Stop visiting rows. They will not be removed.
    Break,
}