LCOV - code coverage report
Current view: top level - libs/walproposer/src - api_bindings.rs (source / functions) Coverage Total Hit
Test: 735481e12b7f1493cbb1bb08e1987cfc6796a660.info Lines: 96.3 % 401 386
Test Date: 2024-06-20 23:27:47 Functions: 97.3 % 37 36

            Line data    Source code
       1              : //! A C-Rust shim: defines implementation of C walproposer API, assuming wp
       2              : //! callback_data stores Box to some Rust implementation.
       3              : 
       4              : #![allow(dead_code)]
       5              : 
       6              : use std::ffi::CStr;
       7              : use std::ffi::CString;
       8              : 
       9              : use crate::bindings::uint32;
      10              : use crate::bindings::walproposer_api;
      11              : use crate::bindings::NeonWALReadResult;
      12              : use crate::bindings::PGAsyncReadResult;
      13              : use crate::bindings::PGAsyncWriteResult;
      14              : use crate::bindings::Safekeeper;
      15              : use crate::bindings::Size;
      16              : use crate::bindings::StringInfoData;
      17              : use crate::bindings::TimestampTz;
      18              : use crate::bindings::WalProposer;
      19              : use crate::bindings::WalProposerConnStatusType;
      20              : use crate::bindings::WalProposerConnectPollStatusType;
      21              : use crate::bindings::WalProposerExecStatusType;
      22              : use crate::bindings::WalproposerShmemState;
      23              : use crate::bindings::XLogRecPtr;
      24              : use crate::walproposer::ApiImpl;
      25              : use crate::walproposer::StreamingCallback;
      26              : use crate::walproposer::WaitResult;
      27              : 
      28        11944 : extern "C" fn get_shmem_state(wp: *mut WalProposer) -> *mut WalproposerShmemState {
      29        11944 :     unsafe {
      30        11944 :         let callback_data = (*(*wp).config).callback_data;
      31        11944 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      32        11944 :         (*api).get_shmem_state()
      33        11944 :     }
      34        11944 : }
      35              : 
      36         1343 : extern "C" fn start_streaming(wp: *mut WalProposer, startpos: XLogRecPtr) {
      37         1343 :     unsafe {
      38         1343 :         let callback_data = (*(*wp).config).callback_data;
      39         1343 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      40         1343 :         let callback = StreamingCallback::new(wp);
      41         1343 :         (*api).start_streaming(startpos, &callback);
      42         1343 :     }
      43         1343 : }
      44              : 
      45          867 : extern "C" fn get_flush_rec_ptr(wp: *mut WalProposer) -> XLogRecPtr {
      46          867 :     unsafe {
      47          867 :         let callback_data = (*(*wp).config).callback_data;
      48          867 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      49          867 :         (*api).get_flush_rec_ptr()
      50          867 :     }
      51          867 : }
      52              : 
      53         7665 : extern "C" fn update_donor(wp: *mut WalProposer, donor: *mut Safekeeper, donor_lsn: XLogRecPtr) {
      54         7665 :     unsafe {
      55         7665 :         let callback_data = (*(*wp).config).callback_data;
      56         7665 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      57         7665 :         (*api).update_donor(&mut (*donor), donor_lsn)
      58         7665 :     }
      59         7665 : }
      60              : 
      61      2571914 : extern "C" fn get_current_timestamp(wp: *mut WalProposer) -> TimestampTz {
      62      2571914 :     unsafe {
      63      2571914 :         let callback_data = (*(*wp).config).callback_data;
      64      2571914 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      65      2571914 :         (*api).get_current_timestamp()
      66      2571914 :     }
      67      2571914 : }
      68              : 
      69        87762 : extern "C" fn conn_error_message(sk: *mut Safekeeper) -> *mut ::std::os::raw::c_char {
      70        87762 :     unsafe {
      71        87762 :         let callback_data = (*(*(*sk).wp).config).callback_data;
      72        87762 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      73        87762 :         let msg = (*api).conn_error_message(&mut (*sk));
      74        87762 :         let msg = CString::new(msg).unwrap();
      75        87762 :         // TODO: fix leaking error message
      76        87762 :         msg.into_raw()
      77        87762 :     }
      78        87762 : }
      79              : 
      80       273705 : extern "C" fn conn_status(sk: *mut Safekeeper) -> WalProposerConnStatusType {
      81       273705 :     unsafe {
      82       273705 :         let callback_data = (*(*(*sk).wp).config).callback_data;
      83       273705 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      84       273705 :         (*api).conn_status(&mut (*sk))
      85       273705 :     }
      86       273705 : }
      87              : 
      88       273705 : extern "C" fn conn_connect_start(sk: *mut Safekeeper) {
      89       273705 :     unsafe {
      90       273705 :         let callback_data = (*(*(*sk).wp).config).callback_data;
      91       273705 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      92       273705 :         (*api).conn_connect_start(&mut (*sk))
      93       273705 :     }
      94       273705 : }
      95              : 
      96       246246 : extern "C" fn conn_connect_poll(sk: *mut Safekeeper) -> WalProposerConnectPollStatusType {
      97       246246 :     unsafe {
      98       246246 :         let callback_data = (*(*(*sk).wp).config).callback_data;
      99       246246 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     100       246246 :         (*api).conn_connect_poll(&mut (*sk))
     101       246246 :     }
     102       246246 : }
     103              : 
     104       246246 : extern "C" fn conn_send_query(sk: *mut Safekeeper, query: *mut ::std::os::raw::c_char) -> bool {
     105       246246 :     let query = unsafe { CStr::from_ptr(query) };
     106       246246 :     let query = query.to_str().unwrap();
     107       246246 : 
     108       246246 :     unsafe {
     109       246246 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     110       246246 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     111       246246 :         (*api).conn_send_query(&mut (*sk), query)
     112       246246 :     }
     113       246246 : }
     114              : 
     115       246246 : extern "C" fn conn_get_query_result(sk: *mut Safekeeper) -> WalProposerExecStatusType {
     116       246246 :     unsafe {
     117       246246 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     118       246246 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     119       246246 :         (*api).conn_get_query_result(&mut (*sk))
     120       246246 :     }
     121       246246 : }
     122              : 
     123            0 : extern "C" fn conn_flush(sk: *mut Safekeeper) -> ::std::os::raw::c_int {
     124            0 :     unsafe {
     125            0 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     126            0 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     127            0 :         (*api).conn_flush(&mut (*sk))
     128            0 :     }
     129            0 : }
     130              : 
     131        90078 : extern "C" fn conn_finish(sk: *mut Safekeeper) {
     132        90078 :     unsafe {
     133        90078 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     134        90078 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     135        90078 :         (*api).conn_finish(&mut (*sk))
     136        90078 :     }
     137        90078 : }
     138              : 
     139       139751 : extern "C" fn conn_async_read(
     140       139751 :     sk: *mut Safekeeper,
     141       139751 :     buf: *mut *mut ::std::os::raw::c_char,
     142       139751 :     amount: *mut ::std::os::raw::c_int,
     143       139751 : ) -> PGAsyncReadResult {
     144       139751 :     unsafe {
     145       139751 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     146       139751 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     147       139751 : 
     148       139751 :         // This function has guarantee that returned buf will be valid until
     149       139751 :         // the next call. So we can store a Vec in each Safekeeper and reuse
     150       139751 :         // it on the next call.
     151       139751 :         let mut inbuf = take_vec_u8(&mut (*sk).inbuf).unwrap_or_default();
     152       139751 :         inbuf.clear();
     153       139751 : 
     154       139751 :         let result = (*api).conn_async_read(&mut (*sk), &mut inbuf);
     155       139751 : 
     156       139751 :         // Put a Vec back to sk->inbuf and return data ptr.
     157       139751 :         *amount = inbuf.len() as i32;
     158       139751 :         *buf = store_vec_u8(&mut (*sk).inbuf, inbuf);
     159       139751 : 
     160       139751 :         result
     161       139751 :     }
     162       139751 : }
     163              : 
     164        35330 : extern "C" fn conn_async_write(
     165        35330 :     sk: *mut Safekeeper,
     166        35330 :     buf: *const ::std::os::raw::c_void,
     167        35330 :     size: usize,
     168        35330 : ) -> PGAsyncWriteResult {
     169        35330 :     unsafe {
     170        35330 :         let buf = std::slice::from_raw_parts(buf as *const u8, size);
     171        35330 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     172        35330 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     173        35330 :         (*api).conn_async_write(&mut (*sk), buf)
     174        35330 :     }
     175        35330 : }
     176              : 
     177       276542 : extern "C" fn conn_blocking_write(
     178       276542 :     sk: *mut Safekeeper,
     179       276542 :     buf: *const ::std::os::raw::c_void,
     180       276542 :     size: usize,
     181       276542 : ) -> bool {
     182       276542 :     unsafe {
     183       276542 :         let buf = std::slice::from_raw_parts(buf as *const u8, size);
     184       276542 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     185       276542 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     186       276542 :         (*api).conn_blocking_write(&mut (*sk), buf)
     187       276542 :     }
     188       276542 : }
     189              : 
     190         5956 : extern "C" fn recovery_download(wp: *mut WalProposer, sk: *mut Safekeeper) -> bool {
     191         5956 :     unsafe {
     192         5956 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     193         5956 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     194         5956 : 
     195         5956 :         // currently `recovery_download` is always called right after election
     196         5956 :         (*api).after_election(&mut (*wp));
     197         5956 : 
     198         5956 :         (*api).recovery_download(&mut (*wp), &mut (*sk))
     199         5956 :     }
     200         5956 : }
     201              : 
     202         8041 : extern "C" fn wal_reader_allocate(sk: *mut Safekeeper) {
     203         8041 :     unsafe {
     204         8041 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     205         8041 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     206         8041 :         (*api).wal_reader_allocate(&mut (*sk));
     207         8041 :     }
     208         8041 : }
     209              : 
     210              : #[allow(clippy::unnecessary_cast)]
     211        27289 : extern "C" fn wal_read(
     212        27289 :     sk: *mut Safekeeper,
     213        27289 :     buf: *mut ::std::os::raw::c_char,
     214        27289 :     startptr: XLogRecPtr,
     215        27289 :     count: Size,
     216        27289 :     _errmsg: *mut *mut ::std::os::raw::c_char,
     217        27289 : ) -> NeonWALReadResult {
     218        27289 :     unsafe {
     219        27289 :         let buf = std::slice::from_raw_parts_mut(buf as *mut u8, count);
     220        27289 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     221        27289 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     222        27289 :         // TODO: errmsg is not forwarded
     223        27289 :         (*api).wal_read(&mut (*sk), buf, startptr)
     224        27289 :     }
     225        27289 : }
     226              : 
     227       115003 : extern "C" fn wal_reader_events(sk: *mut Safekeeper) -> uint32 {
     228       115003 :     unsafe {
     229       115003 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     230       115003 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     231       115003 :         (*api).wal_reader_events(&mut (*sk))
     232       115003 :     }
     233       115003 : }
     234              : 
     235        74284 : extern "C" fn init_event_set(wp: *mut WalProposer) {
     236        74284 :     unsafe {
     237        74284 :         let callback_data = (*(*wp).config).callback_data;
     238        74284 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     239        74284 :         (*api).init_event_set(&mut (*wp));
     240        74284 :     }
     241        74284 : }
     242              : 
     243       544813 : extern "C" fn update_event_set(sk: *mut Safekeeper, events: uint32) {
     244       544813 :     unsafe {
     245       544813 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     246       544813 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     247       544813 :         (*api).update_event_set(&mut (*sk), events);
     248       544813 :     }
     249       544813 : }
     250              : 
     251        40508 : extern "C" fn active_state_update_event_set(sk: *mut Safekeeper) {
     252        40508 :     unsafe {
     253        40508 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     254        40508 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     255        40508 :         (*api).active_state_update_event_set(&mut (*sk));
     256        40508 :     }
     257        40508 : }
     258              : 
     259       492492 : extern "C" fn add_safekeeper_event_set(sk: *mut Safekeeper, events: uint32) {
     260       492492 :     unsafe {
     261       492492 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     262       492492 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     263       492492 :         (*api).add_safekeeper_event_set(&mut (*sk), events);
     264       492492 :     }
     265       492492 : }
     266              : 
     267       308865 : extern "C" fn rm_safekeeper_event_set(sk: *mut Safekeeper) {
     268       308865 :     unsafe {
     269       308865 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     270       308865 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     271       308865 :         (*api).rm_safekeeper_event_set(&mut (*sk));
     272       308865 :     }
     273       308865 : }
     274              : 
     275       716359 : extern "C" fn wait_event_set(
     276       716359 :     wp: *mut WalProposer,
     277       716359 :     timeout: ::std::os::raw::c_long,
     278       716359 :     event_sk: *mut *mut Safekeeper,
     279       716359 :     events: *mut uint32,
     280       716359 : ) -> ::std::os::raw::c_int {
     281       716359 :     unsafe {
     282       716359 :         let callback_data = (*(*wp).config).callback_data;
     283       716359 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     284       716359 :         let result = (*api).wait_event_set(&mut (*wp), timeout);
     285       716359 :         match result {
     286              :             WaitResult::Latch => {
     287        73338 :                 *event_sk = std::ptr::null_mut();
     288        73338 :                 *events = crate::bindings::WL_LATCH_SET;
     289        73338 :                 1
     290              :             }
     291              :             WaitResult::Timeout => {
     292        22230 :                 *event_sk = std::ptr::null_mut();
     293        22230 :                 // WaitEventSetWait returns 0 for timeout.
     294        22230 :                 *events = 0;
     295        22230 :                 0
     296              :             }
     297       620791 :             WaitResult::Network(sk, event_mask) => {
     298       620791 :                 *event_sk = sk;
     299       620791 :                 *events = event_mask;
     300       620791 :                 1
     301              :             }
     302              :         }
     303              :     }
     304       716359 : }
     305              : 
     306        74284 : extern "C" fn strong_random(
     307        74284 :     wp: *mut WalProposer,
     308        74284 :     buf: *mut ::std::os::raw::c_void,
     309        74284 :     len: usize,
     310        74284 : ) -> bool {
     311        74284 :     unsafe {
     312        74284 :         let buf = std::slice::from_raw_parts_mut(buf as *mut u8, len);
     313        74284 :         let callback_data = (*(*wp).config).callback_data;
     314        74284 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     315        74284 :         (*api).strong_random(buf)
     316        74284 :     }
     317        74284 : }
     318              : 
     319         2254 : extern "C" fn get_redo_start_lsn(wp: *mut WalProposer) -> XLogRecPtr {
     320         2254 :     unsafe {
     321         2254 :         let callback_data = (*(*wp).config).callback_data;
     322         2254 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     323         2254 :         (*api).get_redo_start_lsn()
     324         2254 :     }
     325         2254 : }
     326              : 
     327         2891 : extern "C" fn finish_sync_safekeepers(wp: *mut WalProposer, lsn: XLogRecPtr) {
     328         2891 :     unsafe {
     329         2891 :         let callback_data = (*(*wp).config).callback_data;
     330         2891 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     331         2891 :         (*api).finish_sync_safekeepers(lsn)
     332         2891 :     }
     333         2891 : }
     334              : 
     335        14221 : extern "C" fn process_safekeeper_feedback(wp: *mut WalProposer, sk: *mut Safekeeper) {
     336        14221 :     unsafe {
     337        14221 :         let callback_data = (*(*wp).config).callback_data;
     338        14221 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     339        14221 :         (*api).process_safekeeper_feedback(&mut (*wp), &mut (*sk));
     340        14221 :     }
     341        14221 : }
     342              : 
     343       734689 : extern "C" fn log_internal(
     344       734689 :     wp: *mut WalProposer,
     345       734689 :     level: ::std::os::raw::c_int,
     346       734689 :     line: *const ::std::os::raw::c_char,
     347       734689 : ) {
     348       734689 :     unsafe {
     349       734689 :         let callback_data = (*(*wp).config).callback_data;
     350       734689 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     351       734689 :         let line = CStr::from_ptr(line);
     352       734689 :         let line = line.to_str().unwrap();
     353       734689 :         (*api).log_internal(&mut (*wp), Level::from(level as u32), line)
     354       734689 :     }
     355       734689 : }
     356              : 
     357              : #[derive(Debug, PartialEq)]
     358              : pub enum Level {
     359              :     Debug5,
     360              :     Debug4,
     361              :     Debug3,
     362              :     Debug2,
     363              :     Debug1,
     364              :     Log,
     365              :     Info,
     366              :     Notice,
     367              :     Warning,
     368              :     Error,
     369              :     Fatal,
     370              :     Panic,
     371              :     WPEvent,
     372              : }
     373              : 
     374              : impl Level {
     375       734689 :     pub fn from(elevel: u32) -> Level {
     376       734689 :         use crate::bindings::*;
     377       734689 : 
     378       734689 :         match elevel {
     379        27289 :             DEBUG5 => Level::Debug5,
     380            0 :             DEBUG4 => Level::Debug4,
     381            0 :             DEBUG3 => Level::Debug3,
     382        14221 :             DEBUG2 => Level::Debug2,
     383            0 :             DEBUG1 => Level::Debug1,
     384       602112 :             LOG => Level::Log,
     385            0 :             INFO => Level::Info,
     386            0 :             NOTICE => Level::Notice,
     387        90454 :             WARNING => Level::Warning,
     388            0 :             ERROR => Level::Error,
     389          597 :             FATAL => Level::Fatal,
     390           16 :             PANIC => Level::Panic,
     391            0 :             WPEVENT => Level::WPEvent,
     392            0 :             _ => panic!("unknown log level {}", elevel),
     393              :         }
     394       734689 :     }
     395              : }
     396              : 
     397        74284 : pub(crate) fn create_api() -> walproposer_api {
     398        74284 :     walproposer_api {
     399        74284 :         get_shmem_state: Some(get_shmem_state),
     400        74284 :         start_streaming: Some(start_streaming),
     401        74284 :         get_flush_rec_ptr: Some(get_flush_rec_ptr),
     402        74284 :         update_donor: Some(update_donor),
     403        74284 :         get_current_timestamp: Some(get_current_timestamp),
     404        74284 :         conn_error_message: Some(conn_error_message),
     405        74284 :         conn_status: Some(conn_status),
     406        74284 :         conn_connect_start: Some(conn_connect_start),
     407        74284 :         conn_connect_poll: Some(conn_connect_poll),
     408        74284 :         conn_send_query: Some(conn_send_query),
     409        74284 :         conn_get_query_result: Some(conn_get_query_result),
     410        74284 :         conn_flush: Some(conn_flush),
     411        74284 :         conn_finish: Some(conn_finish),
     412        74284 :         conn_async_read: Some(conn_async_read),
     413        74284 :         conn_async_write: Some(conn_async_write),
     414        74284 :         conn_blocking_write: Some(conn_blocking_write),
     415        74284 :         recovery_download: Some(recovery_download),
     416        74284 :         wal_reader_allocate: Some(wal_reader_allocate),
     417        74284 :         wal_read: Some(wal_read),
     418        74284 :         wal_reader_events: Some(wal_reader_events),
     419        74284 :         init_event_set: Some(init_event_set),
     420        74284 :         update_event_set: Some(update_event_set),
     421        74284 :         active_state_update_event_set: Some(active_state_update_event_set),
     422        74284 :         add_safekeeper_event_set: Some(add_safekeeper_event_set),
     423        74284 :         rm_safekeeper_event_set: Some(rm_safekeeper_event_set),
     424        74284 :         wait_event_set: Some(wait_event_set),
     425        74284 :         strong_random: Some(strong_random),
     426        74284 :         get_redo_start_lsn: Some(get_redo_start_lsn),
     427        74284 :         finish_sync_safekeepers: Some(finish_sync_safekeepers),
     428        74284 :         process_safekeeper_feedback: Some(process_safekeeper_feedback),
     429        74284 :         log_internal: Some(log_internal),
     430        74284 :     }
     431        74284 : }
     432              : 
     433        74284 : pub fn empty_shmem() -> crate::bindings::WalproposerShmemState {
     434        74284 :     let empty_feedback = crate::bindings::PageserverFeedback {
     435        74284 :         present: false,
     436        74284 :         currentClusterSize: 0,
     437        74284 :         last_received_lsn: 0,
     438        74284 :         disk_consistent_lsn: 0,
     439        74284 :         remote_consistent_lsn: 0,
     440        74284 :         replytime: 0,
     441        74284 :         shard_number: 0,
     442        74284 :     };
     443        74284 : 
     444        74284 :     crate::bindings::WalproposerShmemState {
     445        74284 :         propEpochStartLsn: crate::bindings::pg_atomic_uint64 { value: 0 },
     446        74284 :         donor_name: [0; 64],
     447        74284 :         donor_conninfo: [0; 1024],
     448        74284 :         donor_lsn: 0,
     449        74284 :         mutex: 0,
     450        74284 :         mineLastElectedTerm: crate::bindings::pg_atomic_uint64 { value: 0 },
     451        74284 :         backpressureThrottlingTime: crate::bindings::pg_atomic_uint64 { value: 0 },
     452        74284 :         currentClusterSize: crate::bindings::pg_atomic_uint64 { value: 0 },
     453        74284 :         shard_ps_feedback: [empty_feedback; 128],
     454        74284 :         num_shards: 0,
     455        74284 :         min_ps_feedback: empty_feedback,
     456        74284 :     }
     457        74284 : }
     458              : 
     459              : impl std::fmt::Display for Level {
     460         2296 :     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
     461         2296 :         write!(f, "{:?}", self)
     462         2296 :     }
     463              : }
     464              : 
     465              : /// Take ownership of `Vec<u8>` from StringInfoData.
     466              : #[allow(clippy::unnecessary_cast)]
     467       362587 : pub(crate) fn take_vec_u8(pg: &mut StringInfoData) -> Option<Vec<u8>> {
     468       362587 :     if pg.data.is_null() {
     469       222846 :         return None;
     470       139741 :     }
     471       139741 : 
     472       139741 :     let ptr = pg.data as *mut u8;
     473       139741 :     let length = pg.len as usize;
     474       139741 :     let capacity = pg.maxlen as usize;
     475       139741 : 
     476       139741 :     pg.data = std::ptr::null_mut();
     477       139741 :     pg.len = 0;
     478       139741 :     pg.maxlen = 0;
     479       139741 : 
     480       139741 :     unsafe { Some(Vec::from_raw_parts(ptr, length, capacity)) }
     481       362587 : }
     482              : 
     483              : /// Store `Vec<u8>` in StringInfoData.
     484       139751 : fn store_vec_u8(pg: &mut StringInfoData, vec: Vec<u8>) -> *mut ::std::os::raw::c_char {
     485       139751 :     let ptr = vec.as_ptr() as *mut ::std::os::raw::c_char;
     486       139751 :     let length = vec.len();
     487       139751 :     let capacity = vec.capacity();
     488       139751 : 
     489       139751 :     assert!(pg.data.is_null());
     490              : 
     491       139751 :     pg.data = ptr;
     492       139751 :     pg.len = length as i32;
     493       139751 :     pg.maxlen = capacity as i32;
     494       139751 : 
     495       139751 :     std::mem::forget(vec);
     496       139751 : 
     497       139751 :     ptr
     498       139751 : }
        

Generated by: LCOV version 2.1-beta