| 1 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::iorust-analyzer cargo sqlx-postgres 0.8.3 io/::PgBufMutExtrust-analyzer cargo sqlx-postgres 0.8.3 io/buf_mut/PgBufMutExt#; |
| 2 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::iorust-analyzer cargo sqlx-postgres 0.8.3 io/::{BufMutExtrust-analyzer cargo sqlx-core 0.8.3 io/buf_mut/BufMutExt#, ProtocolEncoderust-analyzer cargo sqlx-core 0.8.3 io/encode/ProtocolEncode#}; |
| 3 | |
| 4 | // To begin a session, a frontend opens a connection to the server and sends a startup message. |
| 5 | // This message includes the names of the user and of the database the user wants to connect to; |
| 6 | // it also identifies the particular protocol version to be used. |
| 7 | |
| 8 | // Optionally, the startup message can include additional settings for run-time parameters. |
| 9 | |
| 10 | pub struct Startuprust-analyzer cargo sqlx-postgres 0.8.3 message/startup/Startup#<'a> { |
| 11 | /// The database user name to connect as. Required; there is no default. |
| 12 | pub usernamerust-analyzer cargo sqlx-postgres 0.8.3 message/startup/Startup#username.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a str>, |
| 13 | |
| 14 | /// The database to connect to. Defaults to the user name. |
| 15 | pub databaserust-analyzer cargo sqlx-postgres 0.8.3 message/startup/Startup#database.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&'a str>, |
| 16 | |
| 17 | /// Additional start-up params. |
| 18 | /// <https://www.postgresql.org/docs/devel/runtime-config-client.html> |
| 19 | pub paramsrust-analyzer cargo sqlx-postgres 0.8.3 message/startup/Startup#params.: &'a [(&'a str, &'a str)], |
| 20 | } |
| 21 | |
| 22 | // Startup cannot impl FrontendMessage because it doesn't have a format code. |
| 23 | impl ProtocolEncoderust-analyzer cargo sqlx-core 0.8.3 io/encode/ProtocolEncode#<'_> for Startuprust-analyzer cargo sqlx-postgres 0.8.3 message/startup/Startup#<'_> { |
| 24 | fn encode_withrust-analyzer cargo sqlx-postgres 0.8.3 message/startup/impl#[`Startup<'_>`][`ProtocolEncode<'_, ()>`]encode_with().(&self, buf: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, _context: ()) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> { |
| 25 | buf.reserverust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]reserve().(120); |
| 26 | |
| 27 | buf.put_length_prefixedrust-analyzer cargo sqlx-postgres 0.8.3 io/buf_mut/impl#[`Vec<u8>`][PgBufMutExt]put_length_prefixed().(|buf| { |
| 28 | // The protocol version number. The most significant 16 bits are the |
| 29 | // major version number (3 for the protocol described here). The least |
| 30 | // significant 16 bits are the minor version number (0 |
| 31 | // for the protocol described here) |
| 32 | buf.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<&'a T>`]extend().(&196_608_i32.to_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]to_be_bytes().()); |
| 33 | |
| 34 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(username) = self.usernamerust-analyzer cargo sqlx-postgres 0.8.3 message/startup/Startup#username. { |
| 35 | // The database user name to connect as. |
| 36 | encode_startup_paramrust-analyzer cargo sqlx-postgres 0.8.3 message/startup/encode_startup_param().(buf, "user", username); |
| 37 | } |
| 38 | |
| 39 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(database) = self.databaserust-analyzer cargo sqlx-postgres 0.8.3 message/startup/Startup#database. { |
| 40 | // The database to connect to. Defaults to the user name. |
| 41 | encode_startup_paramrust-analyzer cargo sqlx-postgres 0.8.3 message/startup/encode_startup_param().(buf, "database", database); |
| 42 | } |
| 43 | |
| 44 | for (name, value) in self.paramsrust-analyzer cargo sqlx-postgres 0.8.3 message/startup/Startup#params. { |
| 45 | encode_startup_paramrust-analyzer cargo sqlx-postgres 0.8.3 message/startup/encode_startup_param().(buf, name, value); |
| 46 | } |
| 47 | |
| 48 | // A zero byte is required as a terminator |
| 49 | // after the last name/value pair. |
| 50 | buf.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(0); |
| 51 | |
| 52 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 53 | }) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | #[inline] |
| 58 | fn encode_startup_paramrust-analyzer cargo sqlx-postgres 0.8.3 message/startup/encode_startup_param().(buf: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, name: &str, value: &str) { |
| 59 | buf.put_str_nulrust-analyzer cargo sqlx-core 0.8.3 io/buf_mut/impl#[`Vec<u8>`][BufMutExt]put_str_nul().(name); |
| 60 | buf.put_str_nulrust-analyzer cargo sqlx-core 0.8.3 io/buf_mut/impl#[`Vec<u8>`][BufMutExt]put_str_nul().(value); |
| 61 | } |
| 62 | |
| 63 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 64 | fn test_encode_startuprust-analyzer cargo sqlx-postgres 0.8.3 message/startup/test_encode_startup().() { |
| 65 | const EXPECTEDrust-analyzer cargo sqlx-postgres 0.8.3 message/startup/EXPECTED.: &[u8] = b"\0\0\0)\0\x03\0\0user\0postgres\0database\0postgres\0\0"; |
| 66 | |
| 67 | let mut buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 68 | let m = Startuprust-analyzer cargo sqlx-postgres 0.8.3 message/startup/Startup# { |
| 69 | usernamerust-analyzer cargo sqlx-postgres 0.8.3 message/startup/Startup#username.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("postgres"), |
| 70 | databaserust-analyzer cargo sqlx-postgres 0.8.3 message/startup/Startup#database.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("postgres"), |
| 71 | paramsrust-analyzer cargo sqlx-postgres 0.8.3 message/startup/Startup#params.: &[], |
| 72 | }; |
| 73 | |
| 74 | m.encoderust-analyzer cargo sqlx-core 0.8.3 io/encode/ProtocolEncode#encode().(&mut buf).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 75 | |
| 76 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf, EXPECTEDrust-analyzer cargo sqlx-postgres 0.8.3 message/startup/EXPECTED.); |
| 77 | } |
| 78 | |
| 79 | #[cfg(all(test, not(debug_assertions)))] |
| 80 | #[benchrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/bench:] |
| 81 | fn bench_encode_startup(b: &mut test::Bencher) { |
| 82 | use test::black_box; |
| 83 | |
| 84 | let mut buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacity(128); |
| 85 | |
| 86 | b.iter(|| { |
| 87 | buf.clear(); |
| 88 | |
| 89 | black_box(Startuprust-analyzer cargo sqlx-postgres 0.8.3 message/startup/Startup# { |
| 90 | username: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("postgres"), |
| 91 | database: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("postgres"), |
| 92 | params: &[], |
| 93 | }) |
| 94 | .encode(&mut buf); |
| 95 | }); |
| 96 | } |