Skip to content

Commit

Permalink
move types around
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Jan 15, 2024
1 parent b535252 commit 772c2d3
Show file tree
Hide file tree
Showing 29 changed files with 709 additions and 677 deletions.
4 changes: 2 additions & 2 deletions assets_gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use engine::{
SpriteBatchBuilder,
};
use geom::{vec3, InfiniteFrustrum, LinearColor, Plane, Vec2, Vec3};
use prototypes::{load_prototypes, try_prototype};
use prototypes::try_prototype;
use std::path::{Path, PathBuf};

use crate::orbit_camera::OrbitCamera;
Expand Down Expand Up @@ -190,7 +190,7 @@ fn create_shown(gfx: &mut GfxContext, _state: &State, inspected: Inspected) -> S
fn main() {
engine::framework::init();
unsafe {
load_prototypes("./").unwrap();
prototypes::load_prototypes("./").unwrap();
}
engine::framework::start::<State>();
}
16 changes: 16 additions & 0 deletions common/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::error::Error;
use std::fmt::{Display, Formatter};

#[derive(Debug)]
pub struct MultiError<T>(pub Vec<T>);

impl<T: Display> Display for MultiError<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
for e in &self.0 {
writeln!(f, "{}", e)?;
}
Ok(())
}
}

impl<T: Error> Error for MultiError<T> {}
70 changes: 70 additions & 0 deletions common/src/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use rustc_hash::FxHasher;
use std::any::TypeId;
use std::hash::{BuildHasher, Hash, Hasher};

#[inline]
pub fn hash_u64<T>(obj: T) -> u64
where
T: Hash,
{
let mut hasher = FxHasher::default();
obj.hash(&mut hasher);
hasher.finish()
}

#[inline]
/// Hashes the object's type plus content to make sure that the hash is unique even across zero sized types
pub fn hash_type_u64<T>(obj: &T) -> u64
where
T: Hash + 'static,
{
let mut hasher = FxHasher::default();
TypeId::of::<T>().hash(&mut hasher);
obj.hash(&mut hasher);
hasher.finish()
}

pub type FastMap<K, V> = rustc_hash::FxHashMap<K, V>;
pub type FastSet<V> = rustc_hash::FxHashSet<V>;

pub fn fastmap_with_capacity<K, V>(cap: usize) -> FastMap<K, V> {
FastMap::with_capacity_and_hasher(cap, Default::default())
}

pub fn fastset_with_capacity<V>(cap: usize) -> FastSet<V> {
FastSet::with_capacity_and_hasher(cap, Default::default())
}

pub type TransparentMap<K, V> = std::collections::HashMap<K, V, TransparentHasherU64>;

pub fn transparentmap_with_capacity<K, V>(cap: usize) -> TransparentMap<K, V> {
TransparentMap::with_capacity_and_hasher(cap, Default::default())
}

pub fn transparentmap<K, V>() -> TransparentMap<K, V> {
TransparentMap::with_hasher(Default::default())
}
#[derive(Default)]
pub struct TransparentHasherU64(u64);

impl Hasher for TransparentHasherU64 {
fn finish(&self) -> u64 {
self.0
}

fn write(&mut self, _: &[u8]) {
panic!("can only use u64 for transparenthasher")
}

fn write_u64(&mut self, i: u64) {
self.0 = i;
}
}

impl BuildHasher for TransparentHasherU64 {
type Hasher = TransparentHasherU64;

fn build_hasher(&self) -> Self::Hasher {
TransparentHasherU64(self.0)
}
}
249 changes: 19 additions & 230 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,164 +1,29 @@
use std::any::TypeId;
use std::cmp::Ordering;
use std::hash::{BuildHasher, Hash, Hasher};

pub use history::*;

#[macro_export]
macro_rules! unwrap_or {
($e: expr, $t: expr) => {
match $e {
Some(x) => x,
None => $t,
}
};
}

#[macro_export]
macro_rules! assert_ret {
($e: expr) => {
if !$e {
return false;
}
};
}

#[macro_export]
macro_rules! unwrap_ret {
($e: expr) => {
unwrap_ret!($e, ())
};
($e: expr, $ret: expr) => {
match $e {
Some(x) => x,
None => return $ret,
}
};
}

#[macro_export]
macro_rules! unwrap_cont {
($e: expr) => {
match $e {
Some(x) => x,
None => continue,
}
};
}

#[macro_export]
macro_rules! unwrap_orr {
($e: expr, $t: expr) => {
match $e {
Ok(x) => x,
Err(_) => $t,
}
};
}

#[macro_export]
macro_rules! unwrap_retlog {
($e: expr, $($t: expr),+) => {
match $e {
Some(x) => x,
None => {
log::error!($($t),+);
return;
}
}
};
}

#[macro_export]
macro_rules! unwrap_contlog {
($e: expr, $($t: expr),+) => {
match $e {
Some(x) => x,
None => {
log::error!($($t),+);
continue;
}
}
};
}
pub struct DeferFn<V, T: FnOnce() -> V>(pub Option<T>);

impl<V, T: FnOnce() -> V> Drop for DeferFn<V, T> {
fn drop(&mut self) {
(self.0.take().unwrap())();
}
}

#[macro_export]
macro_rules! defer {
($e: expr) => {
let _guard = $crate::DeferFn(Some(move || $e));
};
}

#[macro_export]
macro_rules! assert_delta {
($x:expr, $y:expr, $d:expr) => {
assert!(
$x - $y < $d || $y - $x < $d,
"assert_delta failed: |{} - {}| < {}",
$x,
$y,
$d
);
};
}
mod chunkid;
pub mod error;
mod hash;
pub mod history;
pub mod logger;
pub mod macros;
pub mod rand;
pub mod saveload;
pub mod scroll;
pub mod timestep;

#[macro_export]
macro_rules! defer_serialize {
($me:ty, $defered:ty) => {
impl serde::Serialize for $me {
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>
where
S: serde::Serializer,
{
let v: $defered = self.into();
serde::Serialize::serialize(&v, serializer)
}
}
pub use chunkid::*;
pub use hash::*;

impl<'de> serde::Deserialize<'de> for $me {
fn deserialize<D>(
deserializer: D,
) -> Result<Self, <D as serde::Deserializer<'de>>::Error>
where
D: serde::Deserializer<'de>,
{
let v: $defered = serde::Deserialize::deserialize(deserializer)?;
Ok(v.into())
}
}
};
}
pub use inline_tweak as tw;

#[inline]
pub fn hash_u64<T>(obj: T) -> u64
where
T: Hash,
{
let mut hasher = FxHasher::default();
obj.hash(&mut hasher);
hasher.finish()
pub fn parse_f32(x: &str) -> fast_float::Result<(f32, &str)> {
fast_float::parse_partial::<f32, _>(x)
.map(|(v, l)| (v, if l == x.len() { "" } else { &x[l..] }))
}

#[inline]
/// Hashes the object's type plus content to make sure that the hash is unique even across zero sized types
pub fn hash_type_u64<T>(obj: &T) -> u64
where
T: Hash + 'static,
{
let mut hasher = FxHasher::default();
TypeId::of::<T>().hash(&mut hasher);
obj.hash(&mut hasher);
hasher.finish()
pub fn parse_f64(x: &str) -> fast_float::Result<(f64, &str)> {
fast_float::parse_partial::<f64, _>(x)
.map(|(v, l)| (v, if l == x.len() { "" } else { &x[l..] }))
}

pub struct AccessCmp<'a, T, F>(pub &'a T, pub F);
Expand Down Expand Up @@ -199,79 +64,3 @@ where
U: Eq,
{
}

mod chunkid;
pub mod history;
pub mod logger;
pub mod rand;
pub mod saveload;
pub mod scroll;
pub mod timestep;

pub use chunkid::*;

pub use inline_tweak as tw;
use rustc_hash::FxHasher;

#[derive(Copy, Clone)]
pub enum AudioKind {
Music,
Effect,
Ui,
}

pub type FastMap<K, V> = rustc_hash::FxHashMap<K, V>;
pub type FastSet<V> = rustc_hash::FxHashSet<V>;

pub fn fastmap_with_capacity<K, V>(cap: usize) -> FastMap<K, V> {
FastMap::with_capacity_and_hasher(cap, Default::default())
}

pub fn fastset_with_capacity<V>(cap: usize) -> FastSet<V> {
FastSet::with_capacity_and_hasher(cap, Default::default())
}

pub type TransparentMap<K, V> = std::collections::HashMap<K, V, TransparentHasherU64>;

pub fn transparentmap_with_capacity<K, V>(cap: usize) -> TransparentMap<K, V> {
TransparentMap::with_capacity_and_hasher(cap, Default::default())
}

pub fn transparentmap<K, V>() -> TransparentMap<K, V> {
TransparentMap::with_hasher(Default::default())
}

pub fn parse_f32(x: &str) -> fast_float::Result<(f32, &str)> {
fast_float::parse_partial::<f32, _>(x)
.map(|(v, l)| (v, if l == x.len() { "" } else { &x[l..] }))
}

pub fn parse_f64(x: &str) -> fast_float::Result<(f64, &str)> {
fast_float::parse_partial::<f64, _>(x)
.map(|(v, l)| (v, if l == x.len() { "" } else { &x[l..] }))
}

#[derive(Default)]
pub struct TransparentHasherU64(u64);

impl Hasher for TransparentHasherU64 {
fn finish(&self) -> u64 {
self.0
}

fn write(&mut self, _: &[u8]) {
panic!("can only use u64 for transparenthasher")
}

fn write_u64(&mut self, i: u64) {
self.0 = i;
}
}

impl BuildHasher for TransparentHasherU64 {
type Hasher = TransparentHasherU64;

fn build_hasher(&self) -> Self::Hasher {
TransparentHasherU64(self.0)
}
}
2 changes: 1 addition & 1 deletion common/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl MyLog {

pub fn init() {
let leaked = Box::leak(Box::new(MyLog::new()));
unwrap_orr!(log::set_logger(leaked), return);
crate::unwrap_orr!(log::set_logger(leaked), return);
log::set_max_level(LevelFilter::Debug);
log_panics::init();
}
Expand Down
Loading

0 comments on commit 772c2d3

Please sign in to comment.