Skip to content

Commit 6267ce3

Browse files
authored
fix: remove unsafe rust code (#51)
1 parent a54efb9 commit 6267ce3

File tree

5 files changed

+234
-51
lines changed

5 files changed

+234
-51
lines changed

rust/lib.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl Server {
7373
}
7474

7575
#[napi]
76-
pub async unsafe fn abort(&mut self) {
76+
pub async fn abort(&self) {
7777
self.endpoint.close(0u8.into(), b"");
7878
self.endpoint.wait_idle().await;
7979
self.socket.unbind().await;
@@ -245,31 +245,35 @@ impl Stream {
245245
}
246246

247247
#[napi]
248-
pub async unsafe fn read(&mut self, mut buf: Uint8Array) -> Result<Option<u32>> {
249-
let chunk = self.recv.lock().await.read(buf.as_mut()).await.map_err(to_err)?;
248+
pub async fn read(&self, max_len: u32) -> Result<Option<Uint8Array>> {
249+
let mut buf = vec![0u8; max_len as usize];
250+
let chunk = self.recv.lock().await.read(&mut buf).await.map_err(to_err)?;
250251
match chunk {
251-
Some(len) => Ok(Some(len as u32)),
252+
Some(len) => {
253+
buf.truncate(len);
254+
Ok(Some(buf.into()))
255+
},
252256
None => Ok(None),
253257
}
254258
}
255259

256260
#[napi]
257-
pub async unsafe fn write(&mut self, data: Uint8Array) -> Result<()> {
261+
pub async fn write(&self, data: Uint8Array) -> Result<()> {
258262
self.send.lock().await.write_all(&data).await.map_err(to_err)
259263
}
260264

261265
#[napi]
262-
pub async unsafe fn finish_write(&mut self) {
266+
pub async fn finish_write(&self) {
263267
let _ = self.send.lock().await.finish();
264268
}
265269

266270
#[napi]
267-
pub async unsafe fn reset_write(&mut self) {
271+
pub async fn reset_write(&self) {
268272
let _ = self.send.lock().await.reset(0u8.into());
269273
}
270274

271275
#[napi]
272-
pub async unsafe fn stop_read(&mut self) {
276+
pub async fn stop_read(&self) {
273277
let _ = self.recv.lock().await.stop(0u8.into());
274278
}
275279
}

src/napi.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,7 @@ export declare class Server {
349349

350350
export declare class Stream {
351351
id(): string
352-
read(buf: Uint8Array): Promise<number | null>
353-
read2(): Promise<Uint8Array | null>
352+
read(maxLen: number): Promise<Uint8Array | null>
354353
write(data: Uint8Array): Promise<void>
355354
finishWrite(): Promise<void>
356355
resetWrite(): Promise<void>

0 commit comments

Comments
 (0)