1"use strict";(globalThis.webpackChunkdocs=globalThis.webpackChunkdocs||[]).push([[3345],{27970(e,n,t){t.r(n),t.d(n,{assets:()=>o,contentTitle:()=>l,default:()=>h,frontMatter:()=>a,metadata:()=>i,toc:()=>d});const i=JSON.parse('{"id":"libraries/standard_library/meta/type_def","title":"TypeDefinition","description":"Inspect and transform struct/enum type definitions\u2014fields, generics, attributes, and module context.","source":"@site/versioned_docs/version-v1.0.0-beta.26/libraries/standard_library/meta/type_def.md","sourceDirName":"libraries/standard_library/meta","slug":"/libraries/standard_library/meta/type_def","permalink":"/docs/v1.0.0-beta.26/libraries/standard_library/meta/type_def","draft":false,"unlisted":false,"editUrl":"https://github.com/noir-lang/noir/edit/master/docs/versioned_docs/version-v1.0.0-beta.26/libraries/standard_library/meta/type_def.md","tags":[],"version":"v1.0.0-beta.26","frontMatter":{"title":"TypeDefinition","description":"Inspect and transform struct/enum type definitions\u2014fields, generics, attributes, and module context."},"sidebar":"sidebar","previous":{"title":"Type","permalink":"/docs/v1.0.0-beta.26/libraries/standard_library/meta/typ"},"next":{"title":"TypedExpr","permalink":"/docs/v1.0.0-beta.26/libraries/standard_library/meta/typed_expr"}}');var r=t(74848),s=t(28453);const a={title:"TypeDefinition",description:"Inspect and transform struct/enum type definitions\u2014fields, generics, attributes, and module context."},l=void 0,o={},d=[{value:"Methods",id:"methods",level:2},{value:"add_abi",id:"add_abi",level:3},{value:"as_type",id:"as_type",level:3},{value:"as_type_with_generics",id:"as_type_with_generics",level:3},{value:"generics",id:"generics",level:3},{value:"fields",id:"fields",level:3},{value:"fields_as_written",id:"fields_as_written",level:3},{value:"has_named_attribute",id:"has_named_attribute",level:3},{value:"named_attribute_args",id:"named_attribute_args",level:3},{value:"has_builtin_attribute",id:"has_builtin_attribute",level:3},{value:"location",id:"location",level:3},{value:"module",id:"module",level:3},{value:"name",id:"name",level:3},{value:"Trait Implementations",id:"trait-implementations",level:2}];function c(e){const n={a:"a",blockquote:"blockquote",code:"code",h2:"h2",h3:"h3",p:"p",pre:"pre",sub:"sub",sup:"sup",...(0,s.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)(n.p,{children:[(0,r.jsx)(n.code,{children:"std::meta::type_def"})," contains methods on the built-in ",(0,r.jsx)(n.code,{children:"TypeDefinition"})," type.\nThis type corresponds to ",(0,r.jsx)(n.code,{children:"struct Name { field1: Type1, ... }"})," and ",(0,r.jsx)(n.code,{children:"enum Name { Variant1(Fields1), ... }"})," items in the source program."]}),"\n",(0,r.jsx)(n.h2,{id:"methods",children:"Methods"}),"\n",(0,r.jsx)(n.h3,{id:"add_abi",children:"add_abi"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="add_abi" showLineNumbers ',children:"pub comptime fn add_abi(self, abi_argument: CtString) {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/type_def.nr#L5-L7",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/type_def.nr#L5-L7"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Adds an abi attribute to the data type with the specified argument."}),"\n",(0,r.jsx)(n.h3,{id:"as_type",children:"as_type"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="as_type" showLineNumbers ',children:"pub comptime fn as_type(self) -> Type {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/type_def.nr#L12-L14",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/type_def.nr#L12-L14"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Returns this type definition as a type in the source program. If this definition has\nany generics, the generics are also included as-is."}),"\n",(0,r.jsx)(n.h3,{id:"as_type_with_generics",children:"as_type_with_generics"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="as_type_with_generics" showLineNumbers ',children:"pub comptime fn as_type_with_generics(self, generics: [Type]) -> Option<Type> {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/type_def.nr#L23-L25",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/type_def.nr#L23-L25"})})})}),"\n"]}),"\n",(0,r.jsxs)(n.p,{children:["Returns a type from this type definition using the given generic arguments. Returns ",(0,r.jsx)(n.code,{children:"Option::none()"}),"\nif an incorrect amount of generic arguments are given for this type."]}),"\n",(0,r.jsx)(n.h3,{id:"generics",children:"generics"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="generics" showLineNumbers ',children:"pub comptime fn generics(self) -> [(Type, Option<Type>)] {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/type_def.nr#L49-L51",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/type_def.nr#L49-L51"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Returns each generic on this type definition. Each generic is represented as a tuple containing the type,\nand an optional containing the numeric type if it's a numeric generic."}),"\n",(0,r.jsx)(n.p,{children:"Example:"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{children:"#[example]\nstruct Foo<T, U, let K: u32> {\n bar: [T; K],\n baz: Baz<U, U>,\n}\n\ncomptime fn example(foo: TypeDefinition) {\n assert_eq(foo.generics().len(), 3);\n\n // Fails because `T` isn't in scope\n // let t = quote { T }.as_type();\n // assert_eq(foo.generics()[0].0, t);\n assert(foo.generics()[0].1.is_none());\n\n // Last generic is numeric, so we have the numeric type available to us\n assert(foo.generics()[2].1.is_some());\n}\n"})}),"\n",(0,r.jsx)(n.h3,{id:"fields",children:"fields"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="fields" showLineNumbers ',children:"pub comptime fn fields(self, generic_args: [Type]) -> [(Quoted, Type, Quoted)] {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/type_def.nr#L57-L59",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/type_def.nr#L57-L59"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Returns (name, type, visibility) tuples of each field in this struct type.\nAny generic types used in each field type is automatically substituted with the\nprovided generic arguments."}),"\n",(0,r.jsx)(n.h3,{id:"fields_as_written",children:"fields_as_written"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="fields_as_written" showLineNumbers ',children:"pub comptime fn fields_as_written(self) -> [(Quoted, Type, Quoted)] {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/type_def.nr#L66-L68",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/type_def.nr#L66-L68"})})})}),"\n"]}),"\n",(0,r.jsxs)(n.p,{children:["Returns (name, type, visibility) tuples of each field in this struct type. Each type is as-is\nwith any generic arguments unchanged. Unless the field types are not needed,\nusers should generally prefer to use ",(0,r.jsx)(n.code,{children:"TypeDefinition::fields"})," over this\nfunction if possible."]}),"\n",(0,r.jsx)(n.h3,{id:"has_named_attribute",children:"has_named_attribute"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="has_named_attribute" showLineNumbers ',children:"pub comptime fn has_named_attribute<let N: u32>(self, name: str<N>) -> bool {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/type_def.nr#L28-L30",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/type_def.nr#L28-L30"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Returns true if this type has a custom attribute with the given name."}),"\n",(0,r.jsxs)(n.p,{children:["This matches both built-in attributes and user-written attributes (tags and\napplied comptime macros). Use ",(0,r.jsx)(n.code,{children:"has_builtin_attribute"})," if you need to match\nonly the built-in attribute of the given name."]}),"\n",(0,r.jsx)(n.h3,{id:"named_attribute_args",children:"named_attribute_args"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="named_attribute_args" showLineNumbers ',children:"pub comptime fn named_attribute_args<let N: u32>(self, name: str<N>) -> [[Quoted]] {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/type_def.nr#L33-L35",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/type_def.nr#L33-L35"})})})}),"\n"]}),"\n",(0,r.jsxs)(n.p,{children:["Returns the arguments of each occurrence of the attribute with the given name, as token\nstreams. The outer slice has one entry per occurrence \u2014 empty when the attribute is absent,\nso this is a superset of ",(0,r.jsx)(n.code,{children:"has_named_attribute"})," \u2014 and each inner slice holds that occurrence's\nargument expressions, which can be spliced into generated code with ",(0,r.jsx)(n.code,{children:"quote"}),"."]}),"\n",(0,r.jsx)(n.h3,{id:"has_builtin_attribute",children:"has_builtin_attribute"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="has_builtin_attribute" showLineNumbers ',children:"pub comptime fn has_builtin_attribute<let N: u32>(self, name: str<N>) -> bool {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/type_def.nr#L38-L40",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/type_def.nr#L38-L40"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Returns true if this type has a built-in attribute with the given name."}),"\n",(0,r.jsxs)(n.p,{children:["Unlike ",(0,r.jsx)(n.code,{children:"has_named_attribute"}),", this ignores user-written tag attributes and\napplied comptime macros."]}),"\n",(0,r.jsx)(n.h3,{id:"location",children:"location"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="location" showLineNumbers ',children:"pub comptime fn location(self) -> Location {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/type_def.nr#L42-L44",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/type_def.nr#L42-L44"})})})}),"\n"]}),"\n",(0,r.jsxs)(n.p,{children:["Returns the source ",(0,r.jsx)(n.a,{href:"/docs/v1.0.0-beta.26/libraries/standard_library/meta/location",children:(0,r.jsx)(n.code,{children:"Location"})})," where this type is defined.\nThis can be passed to ",(0,r.jsx)(n.code,{children:"std::meta::error"})," or ",(0,r.jsx)(n.code,{children:"std::meta::warn"})," to attach a diagnostic to the type."]}),"\n",(0,r.jsx)(n.h3,{id:"module",children:"module"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="module" showLineNumbers ',children:"pub comptime fn module(self) -> Module {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/type_def.nr#L71-L73",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/type_def.nr#L71-L73"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Returns the module where the type is defined."}),"\n",(0,r.jsx)(n.h3,{id:"name",children:"name"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",metastring:'title="name" showLineNumbers ',children:"pub comptime fn name(self) -> Quoted {}\n"})}),"\n",(0,r.jsxs)(n.blockquote,{children:["\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.sup,{children:(0,r.jsx)(n.sub,{children:(0,r.jsx)(n.a,{href:"https://github.com/noir-lang/noir/blob/master/noir_stdlib/src/meta/type_def.nr#L76-L78",target:"_blank",rel:"noopener noreferrer",children:"Source code: noir_stdlib/src/meta/type_def.nr#L76-L78"})})})}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:"Returns the name of this type"}),"\n",(0,r.jsx)(n.p,{children:"Note that the returned quoted value will be just the type name, it will\nnot be the full path to the type definition, nor will it include any generics."}),"\n",(0,r.jsx)(n.h2,{id:"trait-implementations",children:"Trait Implementations"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-rust",children:"impl Eq for TypeDefinition\nimpl Hash for TypeDefinition\n"})}),"\n",(0,r.jsx)(n.p,{children:"Note that each type definition is assigned a unique ID internally and this is what is used for\nequality and hashing. So even type definitions with identical generics and fields may not\nbe equal in this sense if they were originally different items in the source program."})]})}function h(e={}){const{wrapper:n}={...(0,s.R)(),...e.components};return n?(0,r.jsx)(n,{...e,children:(0,r.jsx)(c,{...e})}):c(e)}},28453(e,n,t){t.d(n,{R:()=>a,x:()=>l});var i=t(96540);const r={},s=i.createContext(r);function a(e){const n=i.useContext(s);return i.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function l(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:a(e.components),i.createElement(s.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.