Created
May 16, 2019 18:54
-
-
Save DutchGhost/29170c6c8db1415529f37d3b9804dacd to your computer and use it in GitHub Desktop.
debugstruct using const generics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(dead_code, unused_variables)] | |
#![feature(const_generics)] | |
pub type uinfo = u32; | |
pub struct Debug<T, const LINE: uinfo, const COLUMN: uinfo> { | |
pub inner: T, | |
} | |
impl <T, const LINE: uinfo, const COLUMN: uinfo> Debug<T, {LINE}, {COLUMN}> { | |
#[inline(always)] | |
pub const fn line(&self) -> uinfo { LINE } | |
#[inline(always)] | |
pub const fn column(&self) -> uinfo { COLUMN } | |
} | |
pub struct Info { | |
line: uinfo, | |
column: uinfo, | |
} | |
impl Info { | |
pub const fn new(line: uinfo, column: uinfo) -> Self { | |
Self { line, column } | |
} | |
pub const fn line(&self) -> uinfo { self.line } | |
pub const fn column(&self) -> uinfo { self.column } | |
} | |
#[macro_export] | |
macro_rules! debub { | |
($e:expr) => ( | |
{ | |
const INFO: $crate::Info = $crate::Info::new(line!(), column!()); | |
$crate::Debug::<_, {INFO.line()}, {INFO.column()}> { | |
inner: $e | |
} | |
} | |
); | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn it_works() { | |
let x = debub!(10); | |
assert_eq!(x.line(), 52); | |
assert_eq!(x.column(), 17); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment