11use super :: {
2+ drop_boxed,
23 ffi:: {
34 duckdb_bind_add_result_column, duckdb_bind_get_extra_info, duckdb_bind_get_named_parameter,
45 duckdb_bind_get_parameter, duckdb_bind_get_parameter_count, duckdb_bind_info, duckdb_bind_set_bind_data,
@@ -36,6 +37,7 @@ impl BindInfo {
3637 duckdb_bind_add_result_column ( self . ptr , c_str. as_ptr ( ) as * const c_char , column_type. ptr ) ;
3738 }
3839 }
40+
3941 /// Report that an error has occurred while calling bind.
4042 ///
4143 /// # Arguments
@@ -49,18 +51,20 @@ impl BindInfo {
4951 /// Sets the user-provided bind data in the bind object. This object can be retrieved again during execution.
5052 ///
5153 /// # Arguments
52- /// * `extra_data `: The bind data object.
53- /// * `destroy `: The callback that will be called to destroy the bind data (if any)
54+ /// * `data `: The bind data object.
55+ /// * `free_function `: The callback that will be called to destroy the bind data (if any)
5456 ///
5557 /// # Safety
56- ///
58+ /// `data` must be a valid pointer, and `free_function` must properly free it.
5759 pub unsafe fn set_bind_data ( & self , data : * mut c_void , free_function : Option < unsafe extern "C" fn ( * mut c_void ) > ) {
5860 duckdb_bind_set_bind_data ( self . ptr , data, free_function) ;
5961 }
62+
6063 /// Retrieves the number of regular (non-named) parameters to the function.
6164 pub fn get_parameter_count ( & self ) -> u64 {
6265 unsafe { duckdb_bind_get_parameter_count ( self . ptr ) }
6366 }
67+
6468 /// Retrieves the parameter at the given index.
6569 ///
6670 /// # Arguments
@@ -107,10 +111,7 @@ impl BindInfo {
107111 pub fn set_cardinality ( & self , cardinality : idx_t , is_exact : bool ) {
108112 unsafe { duckdb_bind_set_cardinality ( self . ptr , cardinality, is_exact) }
109113 }
110- /// Retrieves the extra info of the function as set in [`TableFunction::set_extra_info`]
111- ///
112- /// # Arguments
113- /// * `returns`: The extra info
114+ /// Retrieves the extra info of the function as set in [`TableFunction::with_extra_info`].
114115 pub fn get_extra_info < T > ( & self ) -> * const T {
115116 unsafe { duckdb_bind_get_extra_info ( self . ptr ) . cast ( ) }
116117 }
@@ -162,13 +163,11 @@ impl InitInfo {
162163 indices
163164 }
164165
165- /// Retrieves the extra info of the function as set in [`TableFunction::set_extra_info`]
166- ///
167- /// # Arguments
168- /// * `returns`: The extra info
166+ /// Retrieves the extra info of the function as set in [`TableFunction::with_extra_info`].
169167 pub fn get_extra_info < T > ( & self ) -> * const T {
170168 unsafe { duckdb_init_get_extra_info ( self . 0 ) . cast ( ) }
171169 }
170+
172171 /// Gets the bind data set by [`BindInfo::set_bind_data`] during the bind.
173172 ///
174173 /// Note that the bind data should be considered as read-only.
@@ -179,13 +178,15 @@ impl InitInfo {
179178 pub fn get_bind_data < T > ( & self ) -> * const T {
180179 unsafe { duckdb_init_get_bind_data ( self . 0 ) . cast ( ) }
181180 }
181+
182182 /// Sets how many threads can process this table function in parallel (default: 1)
183183 ///
184184 /// # Arguments
185185 /// * `max_threads`: The maximum amount of threads that can process this table function
186186 pub fn set_max_threads ( & self , max_threads : idx_t ) {
187187 unsafe { duckdb_init_set_max_threads ( self . 0 , max_threads) }
188188 }
189+
189190 /// Report that an error has occurred while calling init.
190191 ///
191192 /// # Arguments
@@ -307,15 +308,35 @@ impl TableFunction {
307308
308309 /// Assigns extra information to the table function that can be fetched during binding, etc.
309310 ///
311+ /// For most use cases, prefer [`with_extra_info`](Self::with_extra_info) which handles memory management automatically.
312+ ///
310313 /// # Arguments
311314 /// * `extra_info`: The extra information
312315 /// * `destroy`: The callback that will be called to destroy the bind data (if any)
313316 ///
314317 /// # Safety
318+ /// The caller must ensure that `extra_info` is a valid pointer and that `destroy`
319+ /// properly cleans up the data when called.
315320 pub unsafe fn set_extra_info ( & self , extra_info : * mut c_void , destroy : duckdb_delete_callback_t ) {
321+ duckdb_table_function_set_extra_info ( self . ptr , extra_info, destroy) ;
322+ }
323+
324+ /// Assigns extra information to the table function that can be fetched during binding, init, and execution.
325+ ///
326+ /// This is a safe wrapper around [`set_extra_info`](Self::set_extra_info) that handles memory management automatically.
327+ ///
328+ /// # Arguments
329+ /// * `info`: The extra information to store
330+ pub fn with_extra_info < T > ( & self , info : T ) -> & Self
331+ where
332+ T : Send + Sync + ' static ,
333+ {
316334 unsafe {
317- duckdb_table_function_set_extra_info ( self . ptr , extra_info, destroy) ;
335+ let boxed = Box :: new ( info) ;
336+ let ptr = Box :: into_raw ( boxed) as * mut c_void ;
337+ self . set_extra_info ( ptr, Some ( drop_boxed :: < T > ) ) ;
318338 }
339+ self
319340 }
320341
321342 /// Sets the thread-local init function of the table function
@@ -383,13 +404,11 @@ impl<V: VTab> TableFunctionInfo<V> {
383404 }
384405 }
385406
386- /// Retrieves the extra info of the function as set in [`TableFunction::set_extra_info`]
387- ///
388- /// # Arguments
389- /// * `returns`: The extra info
407+ /// Retrieves the extra info of the function as set in [`TableFunction::with_extra_info`].
390408 pub fn get_extra_info < T > ( & self ) -> * mut T {
391409 unsafe { duckdb_function_get_extra_info ( self . ptr ) . cast ( ) }
392410 }
411+
393412 /// Gets the thread-local init data set by [`InitInfo::set_init_data`] during the local_init.
394413 ///
395414 /// # Arguments
0 commit comments