-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26f46ce
commit d877fb3
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::{ | ||
path::Path, | ||
ptr::{self, NonNull}, | ||
sync::Arc | ||
}; | ||
|
||
use crate::{Allocator, Result, ortsys, util}; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct AdapterInner { | ||
pub(crate) ptr: NonNull<ort_sys::OrtLoraAdapter> | ||
} | ||
|
||
impl Drop for AdapterInner { | ||
fn drop(&mut self) { | ||
ortsys![unsafe ReleaseLoraAdapter(self.ptr.as_ptr())]; | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Adapter { | ||
pub(crate) inner: Arc<AdapterInner> | ||
} | ||
|
||
impl Adapter { | ||
pub fn from_file(path: impl AsRef<Path>, allocator: Option<&Allocator>) -> Result<Self> { | ||
let path = util::path_to_os_char(path); | ||
let allocator_ptr = allocator.map(|c| c.ptr()).unwrap_or_else(ptr::null_mut); | ||
let mut ptr = ptr::null_mut(); | ||
ortsys![unsafe CreateLoraAdapter(path.as_ptr(), allocator_ptr, &mut ptr)?]; | ||
Ok(Adapter { | ||
inner: Arc::new(AdapterInner { | ||
ptr: unsafe { NonNull::new_unchecked(ptr) } | ||
}) | ||
}) | ||
} | ||
|
||
pub fn from_memory(bytes: &[u8], allocator: Option<&Allocator>) -> Result<Self> { | ||
let allocator_ptr = allocator.map(|c| c.ptr()).unwrap_or_else(ptr::null_mut); | ||
let mut ptr = ptr::null_mut(); | ||
ortsys![unsafe CreateLoraAdapterFromArray(bytes.as_ptr().cast(), bytes.len(), allocator_ptr, &mut ptr)?]; | ||
Ok(Adapter { | ||
inner: Arc::new(AdapterInner { | ||
ptr: unsafe { NonNull::new_unchecked(ptr) } | ||
}) | ||
}) | ||
} | ||
|
||
pub fn ptr(&self) -> *mut ort_sys::OrtLoraAdapter { | ||
self.inner.ptr.as_ptr() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters