Skip to content

Instantly share code, notes, and snippets.

@DutchGhost
Created May 16, 2019 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DutchGhost/29170c6c8db1415529f37d3b9804dacd to your computer and use it in GitHub Desktop.
Save DutchGhost/29170c6c8db1415529f37d3b9804dacd to your computer and use it in GitHub Desktop.
debugstruct using const generics
#![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