#looking #reflection #instance #glass #enums #look #text

looking-glass

looking-glass is a reflection & type-erasure library for Rust

2 releases

0.1.1 Jan 7, 2022
0.1.0 Jan 7, 2022

#1094 in Text processing

Download history 11/week @ 2024-07-24 9/week @ 2024-07-31 5/week @ 2024-08-07 3/week @ 2024-08-14 2/week @ 2024-08-21 12/week @ 2024-08-28 2/week @ 2024-09-04 66/week @ 2024-09-11 120/week @ 2024-09-18 147/week @ 2024-09-25 73/week @ 2024-10-02 127/week @ 2024-10-09 109/week @ 2024-10-16 7/week @ 2024-10-23 5/week @ 2024-10-30 44/week @ 2024-11-06

192 downloads per month
Used in 4 crates

MIT license

42KB
994 lines

Looking Glass

Looking Glass provides reflection for virtually any Rust type. It does this through a set of traits and type-erasing enums.

This project was recently open-sourced and will have better documentation written up later. For now, the best way to check out the project is to clone it and look at the Rust docs. While this project was only recently open-sourced, it has been iterated on to a point of relative stability. We can't promise that the API won't change, especially for a good reason. But it currently suits all of M10's needs, and so is unlikely to change drastically.

Example

use looking_glass::Typed;
use looking_glass_derive::Instance;

#[derive(Instance, Clone, PartialEq)]
struct Foo {
 text: String,
 int: i32,
}

let test = Foo { text: "Test".to_string(), int: -2 };
let val = test.as_value();
let inst = val.as_reflected_struct().unwrap();
let value  = inst.get_value("text").expect("field not found");
let text = value.as_ref().borrow::<&String>().expect("borrow failed");
assert_eq!(text, &test.text);

Dependencies

~0.4–1MB
~21K SLoC