1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
/*! This library provides abstractions over fields,emulating structural types. # Features These are the features this library provides: - [Derivation of per-field accessor traits](./docs/structural_macro/index.html) (GetField/GetFieldMut/IntoField). - [Declaration of trait aliases for the field accessor traits ](./macro.structural_alias.html). - [Construction of anonymous structs with make_struct](./macro.make_struct.html) # Examples ### Structural Derive This demonstrates how you can use any type with a superset of the fields of another one in a function. For details on the [Structural derive macro look here](./docs/structural_macro/index.html). ```rust use structural::{GetFieldExt,Structural,ti}; #[derive(Structural)] // Using the `#[struc(public)]` attribute tells the derive macro to // generate the accessor trait impls for non-`pub` fields. #[struc(public)] struct Point3D<T>{ x:T, y:T, z:T, } fn reads_point4<S>(point:&S) where // The `Structural` derive macro generated the `Point3D_SI` trait, // aliasing the accessor traits for Point3D. S:Point3D_SI<u32> { let (a,b,c)=point.fields(ti!(x,y,z)); assert_eq!(a,&0); assert_eq!(b,&11); assert_eq!(c,&33); } ////////////////////////////////////////////////////////////////////////// //// In another crate #[derive(Structural)] #[struc(public)] // Using the `#[struc(access="mut move")]` attribute tells the derive macro to // generate the accessor trait for accessing the // fields by reference/mutable-reference/by value, // when by default it only impls the by-reference one. #[struc(access="mut move")] struct Point4D<T>{ x:T, y:T, z:T, a:T, } #[derive(Structural)] #[struc(public)] // Using the `#[struc(access="move")]` attribute tells the derive macro to // generate the accessor trait for accessing the // fields by reference/by value,when by default it only impls the by-reference one. #[struc(access="move")] struct Point5D<T>{ x:T, y:T, z:T, a:T, b:T, } reads_point4(&Point3D { x: 0, y: 11, z: 33 }); reads_point4(&Point4D { x: 0, y: 11, z: 33, a: 0xDEAD, }); reads_point4(&Point5D { x: 0, y: 11, z: 33, a: 0xDEAD, b: 0xBEEF, }); ``` ### Structural alias This demonstrates how you can define a trait alias for a single read-only field accessor. For more details you can look at the docs for the [`structural_alias`](./macro.structural_alias.html) macro. ```rust use structural::{GetFieldExt,Structural,structural_alias,ti}; use std::borrow::Borrow; structural_alias!{ trait Person<S>{ name:S, } } fn print_name<T,S>(this:&T) where T:Person<S>, S:Borrow<str>, { println!("Hello, {}!",this.field_(ti!(name)).borrow() ) } // most structural aliases are object safe fn print_name_dyn< S>(this:&dyn Person<S>) where S:Borrow<str>, { println!("Hello, {}!",this.field_(ti!(name)).borrow() ) } ////////////////////////////////////////////////////////////////////////// //// The stuff here could be defined in a separate crate #[derive(Structural)] // Using the `#[struc(public)]` attribute tells the derive macro to // generate the accessor trait impls for non-`pub` fields. #[struc(public)] struct Worker{ name:String, salary:Cents, } #[derive(Structural)] #[struc(public)] struct Student{ name:String, birth_year:u32, } # #[derive(Debug,Copy,Clone,PartialEq,Eq)] # struct Cents(u64); fn main(){ let worker=Worker{ name:"John Doe".into(), salary:Cents(1_000_000_000_000_000), }; let student=Student{ name:"Jake English".into(), birth_year:1995, }; print_name(&worker); print_name(&student); print_name_dyn(&worker); print_name_dyn(&student); } ``` ### Anonymous structs (`make_struct` macro) This demonstrates how you can construct an anonymous struct. For more details you can look at the docs for the [`make_struct`](./macro.make_struct.html) macro. ```rust use structural::{GetFieldExt,make_struct,structural_alias,ti}; structural_alias!{ trait Person<T>{ // We only have shared access (`&String`) to the field. name:String, // We have shared,mutable,and by value access to the field. mut move value:T, } } fn print_name<T>(mut this:T) where T:Person<Vec<String>>, { println!("Hello, {}!",this.field_(ti!(name)) ); let list=vec!["what".into()]; *this.field_mut(ti!(value))=list.clone(); assert_eq!( this.field_(ti!(value)), &list ); assert_eq!( this.into_field(ti!(value)), list ); } */ #![cfg_attr(feature="alloc",doc=r###" // most structural aliases are object safe fn print_name_dyn(mut this:Box<dyn Person<Vec<String>>>){ println!("Hello, {}!",this.field_(ti!(name)) ); let list=vec!["what".into()]; *this.field_mut(ti!(value))=list.clone(); assert_eq!( this.field_(ti!(value)), &list ); assert_eq!( this.box_into_field(ti!(value)), list ); } "###)] /*! ////////////////////////////////////////////////////////////////////////// //// The stuff here could be defined in a separate crate fn main(){ let worker=make_struct!{ // This derives clone for the anonymous struct #![derive(Clone)] name:"John Doe".into(), salary:Cents(1_000_000_000_000_000), value:vec![], }; let student=make_struct!{ // This derives clone for the anonymous struct #![derive(Clone)] name:"Jake English".into(), birth_year:1995, value:vec![], }; print_name(worker.clone()); print_name(student.clone()); */ #![cfg_attr(feature="alloc",doc=r###" print_name_dyn(Box::new(worker)); print_name_dyn(Box::new(student)); "###)] /*! } #[derive(Debug,Copy,Clone,PartialEq,Eq)] struct Cents(u64); ``` */ #![cfg_attr(feature="nightly_specialization",feature(specialization))] #![cfg_attr(feature="nightly_better_ti",feature(proc_macro_hygiene))] #![cfg_attr(not(feature="alloc"),no_std)] #[doc(hidden)] pub extern crate core as std_; #[doc(hidden)] #[cfg(all(feature="alloc",feature="rust_1_36"))] pub extern crate alloc as alloc_; #[doc(hidden)] #[cfg(all(feature="alloc",feature="rust_1_36"))] pub use alloc_ as alloc; #[doc(hidden)] #[cfg(all(feature="alloc",not(feature="rust_1_36")))] pub use std as alloc; extern crate self as structural; pub use structural_derive::Structural; #[doc(hidden)] pub use structural_derive::{ _ti_impl_, _TI_impl_, structural_alias_impl, declare_name_aliases, }; #[macro_use] mod macros; pub mod docs; pub mod mut_ref; pub mod field_traits; pub mod structural_trait; pub mod type_level; pub mod utils; #[cfg(test)] pub mod tests{ mod structural_derive; mod structural_alias; mod macro_tests; } pub mod chars; pub use crate::{ field_traits::{ GetField,GetFieldMut,IntoField,IntoFieldMut, GetFieldExt, GetFieldType, }, structural_trait::{Structural,StructuralDyn}, }; /// Reexports from the `core_extensions` crate. pub mod reexports{ pub use core_extensions::{MarkerType,SelfOps}; } // pmr(proc macro reexports): // Reexports for the proc macros in structural_derive. #[doc(hidden)] pub mod pmr{ pub use crate::type_level::ident::*; pub use crate::chars::*; pub use core_extensions::MarkerType; }