Skip to main content

plexor_core/
utils.rs

1// Copyright 2025 Alecks Gates
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7//! Utility functions for the plexor-core crate
8
9///
10/// This function takes a generic type T and returns the last part of its type name
11/// by splitting on "::" and taking the last element. This is useful for getting
12/// just the struct name without the full module path.
13///
14/// # Examples
15///
16/// ```
17/// use plexor_core::utils::struct_name_of_type;
18///
19/// struct MyStruct;
20/// assert_eq!(struct_name_of_type::<MyStruct>(), "MyStruct");
21/// ```
22#[must_use]
23pub fn struct_name_of_type<T: ?Sized>() -> &'static str {
24    let type_name = std::any::type_name::<T>();
25    type_name.split("::").last().unwrap()
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    struct TestStruct;
33
34    #[test]
35    fn test_struct_name_of_type() {
36        assert_eq!(struct_name_of_type::<TestStruct>(), "TestStruct");
37        assert_eq!(struct_name_of_type::<String>(), "String");
38        assert_eq!(struct_name_of_type::<Vec<i32>>(), "Vec<i32>");
39    }
40}