Skip to main content

Metadata

Struct Metadata 

Source
pub struct Metadata(/* private fields */);
Expand description

A safe wrapper around a Swift type metadata pointer.

Implementations§

Source§

impl Metadata

Source

pub fn from_raw(ptr: *const c_void) -> Option<Self>

Create from a raw pointer. Returns None if null.

Source

pub fn as_raw(&self) -> *const c_void

Get the raw pointer.

Source

pub fn kind(&self) -> MetadataKind

Get the metadata kind.

Examples found in repository?
examples/type_inspector.rs (line 24)
7fn main() {
8    println!("=== Swift Type Inspector ===\n");
9
10    // Primitive types
11    let primitives = [
12        ("Int", types::int()),
13        ("Bool", types::bool()),
14        ("Double", types::double()),
15        ("Float", types::float()),
16        ("String", types::string()),
17    ];
18
19    println!("Primitive types:");
20    for (name, meta) in &primitives {
21        if let Some(m) = meta {
22            println!(
23                "  {name:8} kind={:?} size={} stride={} align={} pod={}",
24                m.kind(),
25                m.size(),
26                m.stride(),
27                m.alignment(),
28                m.is_pod(),
29            );
30        }
31    }
32
33    // Type names
34    println!("\nQualified names:");
35    for (_, meta) in &primitives {
36        if let Some(m) = meta {
37            println!("  {}", m.type_name(true).unwrap_or("?".into()));
38        }
39    }
40
41    // Descriptor names
42    println!("\nDescriptor names:");
43    for (_, meta) in &primitives {
44        if let Some(m) = meta {
45            println!("  {}", m.descriptor_name().unwrap_or("?".into()));
46        }
47    }
48
49    // Generic types
50    println!("\nGeneric types:");
51    let int = types::int().unwrap();
52    let str = types::string().unwrap();
53
54    if let Some(opt) = types::optional(&int) {
55        println!("  Optional<Int>: kind={:?} size={}", opt.kind(), opt.size());
56    }
57    if let Some(arr) = types::array(&int) {
58        println!("  Array<Int>:    kind={:?} size={}", arr.kind(), arr.size());
59    }
60    if let Some(dict) = types::dictionary(&str, &int) {
61        println!(
62            "  Dict<Str,Int>: kind={:?} size={}",
63            dict.kind(),
64            dict.size()
65        );
66    }
67
68    // Mangled name lookup
69    println!("\nMangled name lookup:");
70    if let Some(m) = metadata::lookup_type(b"Sf") {
71        println!(
72            "  'Sf' → {} (size={})",
73            m.type_name(true).unwrap_or("?".into()),
74            m.size()
75        );
76    }
77
78    // Debug format
79    println!("\nDebug format:");
80    println!("  {:?}", types::int().unwrap());
81    println!("  {:?}", types::string().unwrap());
82
83    // Small string
84    println!("\nSmall string:");
85    if let Some(buf) = swift_runtime::string::create_small("hello") {
86        if let Some(s) = swift_runtime::string::extract_small(&buf) {
87            println!("  Created and extracted: '{s}'");
88        }
89    }
90}
Source

pub fn value_witness_table(&self) -> &ValueWitnessTable

Get the value witness table.

Source

pub fn size(&self) -> usize

Get the size of values of this type.

Examples found in repository?
examples/type_inspector.rs (line 25)
7fn main() {
8    println!("=== Swift Type Inspector ===\n");
9
10    // Primitive types
11    let primitives = [
12        ("Int", types::int()),
13        ("Bool", types::bool()),
14        ("Double", types::double()),
15        ("Float", types::float()),
16        ("String", types::string()),
17    ];
18
19    println!("Primitive types:");
20    for (name, meta) in &primitives {
21        if let Some(m) = meta {
22            println!(
23                "  {name:8} kind={:?} size={} stride={} align={} pod={}",
24                m.kind(),
25                m.size(),
26                m.stride(),
27                m.alignment(),
28                m.is_pod(),
29            );
30        }
31    }
32
33    // Type names
34    println!("\nQualified names:");
35    for (_, meta) in &primitives {
36        if let Some(m) = meta {
37            println!("  {}", m.type_name(true).unwrap_or("?".into()));
38        }
39    }
40
41    // Descriptor names
42    println!("\nDescriptor names:");
43    for (_, meta) in &primitives {
44        if let Some(m) = meta {
45            println!("  {}", m.descriptor_name().unwrap_or("?".into()));
46        }
47    }
48
49    // Generic types
50    println!("\nGeneric types:");
51    let int = types::int().unwrap();
52    let str = types::string().unwrap();
53
54    if let Some(opt) = types::optional(&int) {
55        println!("  Optional<Int>: kind={:?} size={}", opt.kind(), opt.size());
56    }
57    if let Some(arr) = types::array(&int) {
58        println!("  Array<Int>:    kind={:?} size={}", arr.kind(), arr.size());
59    }
60    if let Some(dict) = types::dictionary(&str, &int) {
61        println!(
62            "  Dict<Str,Int>: kind={:?} size={}",
63            dict.kind(),
64            dict.size()
65        );
66    }
67
68    // Mangled name lookup
69    println!("\nMangled name lookup:");
70    if let Some(m) = metadata::lookup_type(b"Sf") {
71        println!(
72            "  'Sf' → {} (size={})",
73            m.type_name(true).unwrap_or("?".into()),
74            m.size()
75        );
76    }
77
78    // Debug format
79    println!("\nDebug format:");
80    println!("  {:?}", types::int().unwrap());
81    println!("  {:?}", types::string().unwrap());
82
83    // Small string
84    println!("\nSmall string:");
85    if let Some(buf) = swift_runtime::string::create_small("hello") {
86        if let Some(s) = swift_runtime::string::extract_small(&buf) {
87            println!("  Created and extracted: '{s}'");
88        }
89    }
90}
Source

pub fn stride(&self) -> usize

Get the stride of values of this type.

Examples found in repository?
examples/type_inspector.rs (line 26)
7fn main() {
8    println!("=== Swift Type Inspector ===\n");
9
10    // Primitive types
11    let primitives = [
12        ("Int", types::int()),
13        ("Bool", types::bool()),
14        ("Double", types::double()),
15        ("Float", types::float()),
16        ("String", types::string()),
17    ];
18
19    println!("Primitive types:");
20    for (name, meta) in &primitives {
21        if let Some(m) = meta {
22            println!(
23                "  {name:8} kind={:?} size={} stride={} align={} pod={}",
24                m.kind(),
25                m.size(),
26                m.stride(),
27                m.alignment(),
28                m.is_pod(),
29            );
30        }
31    }
32
33    // Type names
34    println!("\nQualified names:");
35    for (_, meta) in &primitives {
36        if let Some(m) = meta {
37            println!("  {}", m.type_name(true).unwrap_or("?".into()));
38        }
39    }
40
41    // Descriptor names
42    println!("\nDescriptor names:");
43    for (_, meta) in &primitives {
44        if let Some(m) = meta {
45            println!("  {}", m.descriptor_name().unwrap_or("?".into()));
46        }
47    }
48
49    // Generic types
50    println!("\nGeneric types:");
51    let int = types::int().unwrap();
52    let str = types::string().unwrap();
53
54    if let Some(opt) = types::optional(&int) {
55        println!("  Optional<Int>: kind={:?} size={}", opt.kind(), opt.size());
56    }
57    if let Some(arr) = types::array(&int) {
58        println!("  Array<Int>:    kind={:?} size={}", arr.kind(), arr.size());
59    }
60    if let Some(dict) = types::dictionary(&str, &int) {
61        println!(
62            "  Dict<Str,Int>: kind={:?} size={}",
63            dict.kind(),
64            dict.size()
65        );
66    }
67
68    // Mangled name lookup
69    println!("\nMangled name lookup:");
70    if let Some(m) = metadata::lookup_type(b"Sf") {
71        println!(
72            "  'Sf' → {} (size={})",
73            m.type_name(true).unwrap_or("?".into()),
74            m.size()
75        );
76    }
77
78    // Debug format
79    println!("\nDebug format:");
80    println!("  {:?}", types::int().unwrap());
81    println!("  {:?}", types::string().unwrap());
82
83    // Small string
84    println!("\nSmall string:");
85    if let Some(buf) = swift_runtime::string::create_small("hello") {
86        if let Some(s) = swift_runtime::string::extract_small(&buf) {
87            println!("  Created and extracted: '{s}'");
88        }
89    }
90}
Source

pub fn alignment(&self) -> usize

Get the alignment of values of this type.

Examples found in repository?
examples/type_inspector.rs (line 27)
7fn main() {
8    println!("=== Swift Type Inspector ===\n");
9
10    // Primitive types
11    let primitives = [
12        ("Int", types::int()),
13        ("Bool", types::bool()),
14        ("Double", types::double()),
15        ("Float", types::float()),
16        ("String", types::string()),
17    ];
18
19    println!("Primitive types:");
20    for (name, meta) in &primitives {
21        if let Some(m) = meta {
22            println!(
23                "  {name:8} kind={:?} size={} stride={} align={} pod={}",
24                m.kind(),
25                m.size(),
26                m.stride(),
27                m.alignment(),
28                m.is_pod(),
29            );
30        }
31    }
32
33    // Type names
34    println!("\nQualified names:");
35    for (_, meta) in &primitives {
36        if let Some(m) = meta {
37            println!("  {}", m.type_name(true).unwrap_or("?".into()));
38        }
39    }
40
41    // Descriptor names
42    println!("\nDescriptor names:");
43    for (_, meta) in &primitives {
44        if let Some(m) = meta {
45            println!("  {}", m.descriptor_name().unwrap_or("?".into()));
46        }
47    }
48
49    // Generic types
50    println!("\nGeneric types:");
51    let int = types::int().unwrap();
52    let str = types::string().unwrap();
53
54    if let Some(opt) = types::optional(&int) {
55        println!("  Optional<Int>: kind={:?} size={}", opt.kind(), opt.size());
56    }
57    if let Some(arr) = types::array(&int) {
58        println!("  Array<Int>:    kind={:?} size={}", arr.kind(), arr.size());
59    }
60    if let Some(dict) = types::dictionary(&str, &int) {
61        println!(
62            "  Dict<Str,Int>: kind={:?} size={}",
63            dict.kind(),
64            dict.size()
65        );
66    }
67
68    // Mangled name lookup
69    println!("\nMangled name lookup:");
70    if let Some(m) = metadata::lookup_type(b"Sf") {
71        println!(
72            "  'Sf' → {} (size={})",
73            m.type_name(true).unwrap_or("?".into()),
74            m.size()
75        );
76    }
77
78    // Debug format
79    println!("\nDebug format:");
80    println!("  {:?}", types::int().unwrap());
81    println!("  {:?}", types::string().unwrap());
82
83    // Small string
84    println!("\nSmall string:");
85    if let Some(buf) = swift_runtime::string::create_small("hello") {
86        if let Some(s) = swift_runtime::string::extract_small(&buf) {
87            println!("  Created and extracted: '{s}'");
88        }
89    }
90}
Source

pub fn is_pod(&self) -> bool

Check if this type is POD (trivially copyable/destroyable).

Examples found in repository?
examples/type_inspector.rs (line 28)
7fn main() {
8    println!("=== Swift Type Inspector ===\n");
9
10    // Primitive types
11    let primitives = [
12        ("Int", types::int()),
13        ("Bool", types::bool()),
14        ("Double", types::double()),
15        ("Float", types::float()),
16        ("String", types::string()),
17    ];
18
19    println!("Primitive types:");
20    for (name, meta) in &primitives {
21        if let Some(m) = meta {
22            println!(
23                "  {name:8} kind={:?} size={} stride={} align={} pod={}",
24                m.kind(),
25                m.size(),
26                m.stride(),
27                m.alignment(),
28                m.is_pod(),
29            );
30        }
31    }
32
33    // Type names
34    println!("\nQualified names:");
35    for (_, meta) in &primitives {
36        if let Some(m) = meta {
37            println!("  {}", m.type_name(true).unwrap_or("?".into()));
38        }
39    }
40
41    // Descriptor names
42    println!("\nDescriptor names:");
43    for (_, meta) in &primitives {
44        if let Some(m) = meta {
45            println!("  {}", m.descriptor_name().unwrap_or("?".into()));
46        }
47    }
48
49    // Generic types
50    println!("\nGeneric types:");
51    let int = types::int().unwrap();
52    let str = types::string().unwrap();
53
54    if let Some(opt) = types::optional(&int) {
55        println!("  Optional<Int>: kind={:?} size={}", opt.kind(), opt.size());
56    }
57    if let Some(arr) = types::array(&int) {
58        println!("  Array<Int>:    kind={:?} size={}", arr.kind(), arr.size());
59    }
60    if let Some(dict) = types::dictionary(&str, &int) {
61        println!(
62            "  Dict<Str,Int>: kind={:?} size={}",
63            dict.kind(),
64            dict.size()
65        );
66    }
67
68    // Mangled name lookup
69    println!("\nMangled name lookup:");
70    if let Some(m) = metadata::lookup_type(b"Sf") {
71        println!(
72            "  'Sf' → {} (size={})",
73            m.type_name(true).unwrap_or("?".into()),
74            m.size()
75        );
76    }
77
78    // Debug format
79    println!("\nDebug format:");
80    println!("  {:?}", types::int().unwrap());
81    println!("  {:?}", types::string().unwrap());
82
83    // Small string
84    println!("\nSmall string:");
85    if let Some(buf) = swift_runtime::string::create_small("hello") {
86        if let Some(s) = swift_runtime::string::extract_small(&buf) {
87            println!("  Created and extracted: '{s}'");
88        }
89    }
90}
Source

pub fn type_name(&self, qualified: bool) -> Option<String>

Get the human-readable type name.

Examples found in repository?
examples/type_inspector.rs (line 37)
7fn main() {
8    println!("=== Swift Type Inspector ===\n");
9
10    // Primitive types
11    let primitives = [
12        ("Int", types::int()),
13        ("Bool", types::bool()),
14        ("Double", types::double()),
15        ("Float", types::float()),
16        ("String", types::string()),
17    ];
18
19    println!("Primitive types:");
20    for (name, meta) in &primitives {
21        if let Some(m) = meta {
22            println!(
23                "  {name:8} kind={:?} size={} stride={} align={} pod={}",
24                m.kind(),
25                m.size(),
26                m.stride(),
27                m.alignment(),
28                m.is_pod(),
29            );
30        }
31    }
32
33    // Type names
34    println!("\nQualified names:");
35    for (_, meta) in &primitives {
36        if let Some(m) = meta {
37            println!("  {}", m.type_name(true).unwrap_or("?".into()));
38        }
39    }
40
41    // Descriptor names
42    println!("\nDescriptor names:");
43    for (_, meta) in &primitives {
44        if let Some(m) = meta {
45            println!("  {}", m.descriptor_name().unwrap_or("?".into()));
46        }
47    }
48
49    // Generic types
50    println!("\nGeneric types:");
51    let int = types::int().unwrap();
52    let str = types::string().unwrap();
53
54    if let Some(opt) = types::optional(&int) {
55        println!("  Optional<Int>: kind={:?} size={}", opt.kind(), opt.size());
56    }
57    if let Some(arr) = types::array(&int) {
58        println!("  Array<Int>:    kind={:?} size={}", arr.kind(), arr.size());
59    }
60    if let Some(dict) = types::dictionary(&str, &int) {
61        println!(
62            "  Dict<Str,Int>: kind={:?} size={}",
63            dict.kind(),
64            dict.size()
65        );
66    }
67
68    // Mangled name lookup
69    println!("\nMangled name lookup:");
70    if let Some(m) = metadata::lookup_type(b"Sf") {
71        println!(
72            "  'Sf' → {} (size={})",
73            m.type_name(true).unwrap_or("?".into()),
74            m.size()
75        );
76    }
77
78    // Debug format
79    println!("\nDebug format:");
80    println!("  {:?}", types::int().unwrap());
81    println!("  {:?}", types::string().unwrap());
82
83    // Small string
84    println!("\nSmall string:");
85    if let Some(buf) = swift_runtime::string::create_small("hello") {
86        if let Some(s) = swift_runtime::string::extract_small(&buf) {
87            println!("  Created and extracted: '{s}'");
88        }
89    }
90}
Source

pub fn descriptor(&self) -> Option<*const c_void>

Get the type context descriptor.

Source

pub fn descriptor_name(&self) -> Option<String>

Get the descriptor name (e.g., “Int”, “String”).

Examples found in repository?
examples/type_inspector.rs (line 45)
7fn main() {
8    println!("=== Swift Type Inspector ===\n");
9
10    // Primitive types
11    let primitives = [
12        ("Int", types::int()),
13        ("Bool", types::bool()),
14        ("Double", types::double()),
15        ("Float", types::float()),
16        ("String", types::string()),
17    ];
18
19    println!("Primitive types:");
20    for (name, meta) in &primitives {
21        if let Some(m) = meta {
22            println!(
23                "  {name:8} kind={:?} size={} stride={} align={} pod={}",
24                m.kind(),
25                m.size(),
26                m.stride(),
27                m.alignment(),
28                m.is_pod(),
29            );
30        }
31    }
32
33    // Type names
34    println!("\nQualified names:");
35    for (_, meta) in &primitives {
36        if let Some(m) = meta {
37            println!("  {}", m.type_name(true).unwrap_or("?".into()));
38        }
39    }
40
41    // Descriptor names
42    println!("\nDescriptor names:");
43    for (_, meta) in &primitives {
44        if let Some(m) = meta {
45            println!("  {}", m.descriptor_name().unwrap_or("?".into()));
46        }
47    }
48
49    // Generic types
50    println!("\nGeneric types:");
51    let int = types::int().unwrap();
52    let str = types::string().unwrap();
53
54    if let Some(opt) = types::optional(&int) {
55        println!("  Optional<Int>: kind={:?} size={}", opt.kind(), opt.size());
56    }
57    if let Some(arr) = types::array(&int) {
58        println!("  Array<Int>:    kind={:?} size={}", arr.kind(), arr.size());
59    }
60    if let Some(dict) = types::dictionary(&str, &int) {
61        println!(
62            "  Dict<Str,Int>: kind={:?} size={}",
63            dict.kind(),
64            dict.size()
65        );
66    }
67
68    // Mangled name lookup
69    println!("\nMangled name lookup:");
70    if let Some(m) = metadata::lookup_type(b"Sf") {
71        println!(
72            "  'Sf' → {} (size={})",
73            m.type_name(true).unwrap_or("?".into()),
74            m.size()
75        );
76    }
77
78    // Debug format
79    println!("\nDebug format:");
80    println!("  {:?}", types::int().unwrap());
81    println!("  {:?}", types::string().unwrap());
82
83    // Small string
84    println!("\nSmall string:");
85    if let Some(buf) = swift_runtime::string::create_small("hello") {
86        if let Some(s) = swift_runtime::string::extract_small(&buf) {
87            println!("  Created and extracted: '{s}'");
88        }
89    }
90}

Trait Implementations§

Source§

impl Clone for Metadata

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Metadata

Source§

impl Debug for Metadata

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.