From 1786efd3a6d9ed69688e2aed69ec8cc644988828 Mon Sep 17 00:00:00 2001
From: Lars Kanis <lars.kanis@sincnovation.com>
Date: Wed, 12 Feb 2025 15:13:35 +0100
Subject: [PATCH 05/10] Fix warning about deprecated access to fptr->fd

Warning was:
   warning: 'fd' is deprecated: rb_io_descriptor
---
 ext/native/extconf.rb              |  1 +
 ext/native/posix_serialport_impl.c | 13 ++++++++-----
 ext/native/win_serialport_impl.c   | 14 ++++++++------
 3 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/ext/native/extconf.rb b/ext/native/extconf.rb
index 3b384af..df36284 100644
--- a/ext/native/extconf.rb
+++ b/ext/native/extconf.rb
@@ -10,6 +10,7 @@ if !(os == 'mswin' or os == 'bccwin' or os == 'mingw')
   exit(1) if not have_header("termios.h") or not have_header("unistd.h")
 end
 
+have_func("rb_io_descriptor") # ruby-3.1+
 have_func("rb_io_open_descriptor") # ruby-3.3+
 
 create_makefile('serialport')
diff --git a/ext/native/posix_serialport_impl.c b/ext/native/posix_serialport_impl.c
index fe0fc57..c096e08 100644
--- a/ext/native/posix_serialport_impl.c
+++ b/ext/native/posix_serialport_impl.c
@@ -61,12 +61,15 @@ static char sTcsetattr[] = "tcsetattr";
 static char sIoctl[] = "ioctl";
 
 
-int get_fd_helper(obj)
-   VALUE obj;
+int get_fd_helper(VALUE io)
 {
-   rb_io_t *fptr;
-   GetOpenFile(obj, fptr);
-   return (fptr->fd);
+#ifdef HAVE_RB_IO_DESCRIPTOR
+  return rb_io_descriptor(io);
+#else
+  rb_io_t* fp;
+  GetOpenFile(io, fp);
+  return fp->fd;
+#endif
 }
 
 VALUE sp_create_impl(class, _port)
diff --git a/ext/native/win_serialport_impl.c b/ext/native/win_serialport_impl.c
index 781bbc1..26c5499 100644
--- a/ext/native/win_serialport_impl.c
+++ b/ext/native/win_serialport_impl.c
@@ -33,13 +33,15 @@ static char sGetCommTimeouts[] = "GetCommTimeouts";
 static char sSetCommTimeouts[] = "SetCommTimeouts";
 
 
-static HANDLE get_handle_helper(obj)
-   VALUE obj;
+static HANDLE get_handle_helper(VALUE io)
 {
-   rb_io_t *fptr;
-
-   GetOpenFile(obj, fptr);
-   return (HANDLE) _get_osfhandle(fptr->fd);
+#ifdef HAVE_RB_IO_DESCRIPTOR
+  return (HANDLE) _get_osfhandle(rb_io_descriptor(io));
+#else
+  rb_io_t* fp;
+  GetOpenFile(io, fp);
+  return (HANDLE) _get_osfhandle(fp->fd);
+#endif
 }
 
 /* hack to work around the fact that Ruby doesn't use GetLastError? */
-- 
2.39.5

