tyname

Crates.iotyname
lib.rstyname
version0.1.0
sourcesrc
created_at2018-11-27 15:32:11.631789
updated_at2018-11-27 15:32:11.631789
descriptionRetrieve type names during program execution on stable Rust.
homepagehttps://github.com/robbepop/tyname
repositoryhttps://github.com/robbepop/tyname
max_upload_size
id98941
size28,335
Robin Freyler (Robbepop)

documentation

https://docs.rs/tyname/

README

tyname - Type names on stable Rust

Docs Crates.io
docs crates

WORKS ON STABLE RUST

Retrieve type names during program execution on stable Rust.

Other solutions in the Rust ecosystem use the unstable core::intrinsics::type_name API.

Examples

Use

Works for every built-in type.

assert_eq!(type_name::<()>(), String::from("()"));
assert_eq!(type_name::<i32>(), String::from("i32"));
assert_eq!(type_name::<[u8; 32]>(), String::from("[u8; 32]"));

Works for tuples up to 10 different fields.

assert_eq!(
	type_name::<(i8, i16, i32, i64, i128)>(),
	String::from("(i8, i16, i32, i64, i128)")
);

Works on structs.

assert_eq!(
	type_name::<Vec<u8>>(), String::from("Vec<u8>")
);
assert_eq!(
	type_name::<Result<i32, String>>(),
	String::from("Result<i32, String>")
);

Works on function pointer types.

assert_eq!(
	type_name::<fn(i32, f32) -> bool>(),
	String::from("fn(i32, f32) -> bool")
);
assert_eq!(
	type_name::<fn()>(),
	String::from("fn() -> ()")
);

Implement

The TypeName trait is used for retrieving the names. Every type that implements it can be used. This library already implements the most common types for the users

Users can implement it manually for their own types, too.

Note: A derive functionality is planned but not yet crafted.

/// The type we want to make work for the `TypeName` trait
struct Foo<T1, T2> { a: T1, b: T2 }

/// The manual implementation.
impl<T1, T2> crate::TypeName for Foo<T1, T2>
where
	T1: TypeName,
	T2: TypeName,
{
	fn write_type_name<W>(w: &mut W) -> std::fmt::Result
	where
		W: std::fmt::Write
	{
		w.write_str("Foo<")?;
		T1::write_type_name(w)?;
		w.write_str(", ")?;
		T2::write_type_name(w)?;
		w.write_char('>')
	}
}

fn main() {
	assert_eq!(
		type_name::<Foo<bool, char>>(),
		String::from("Foo<bool, char>")
	);
}

Future

With a #[derive(TypeName)] functionality it will be possible to implement this for custom types such as the struct Foo above like the following.

#[derive(TypeName)]
struct Foo<T1, T2>{
	a: T1,
	b: T2,
}

Done.

Short comings

  • No #[derive(TypeName)] functionality provided, yet.
  • Requires computation at run-time compared to the core::intrinsics::type_name API
  • Cannot print out the paths to the type, e.g. the std::result:: in std::result::Result<T, E>.
  • Currently has problems with function pointer types returning the unit type, e.g. fn() will print fn() -> () instead.

License

Licensed under either of

at your option.

Dual licence: badge badge

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 10

cargo fmt