Skip to content

Commit

Permalink
Merge pull request #83 from byeongkeunahn/update-examples
Browse files Browse the repository at this point in the history
Update examples
  • Loading branch information
kiwiyou authored Feb 22, 2024
2 parents 5af62af + 3a6388e commit 98f2d0b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
40 changes: 19 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ fn main() {
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
let mut input = s.split_whitespace().flat_map(str::parse);
let a: usize = input.next().unwrap();
let b: usize = input.next().unwrap();
let a: i64 = input.next().unwrap();
let b: i64 = input.next().unwrap();
println!("{}", a + b);
}
```
Expand All @@ -51,13 +51,13 @@ fn main() {
```rust
// basm/src/solution.rs
use basm::platform::io::{Reader, ReaderTrait, Writer};
use basm::platform::io::{Reader, ReaderTrait, Writer, Print};
pub fn main() {
let mut reader: Reader = Default::default();
let mut writer: Writer = Default::default();
let a = reader.usize();
let b = reader.usize();
writer.usize(a + b);
let a = reader.i64();
let b = reader.i64();
writer.println(a + b);
}
```

Expand All @@ -70,9 +70,9 @@ use basm::platform::io::{Reader, ReaderTrait, Writer};
pub fn main() {
let mut reader = Reader::<128>::new();
let mut writer = Writer::<128>::new();
let a = reader.usize();
let b = reader.usize();
writer.usize(a + b);
let a = reader.i64();
let b = reader.i64();
writer.i64(a + b);
}
```

Expand Down Expand Up @@ -215,7 +215,7 @@ dashu = { git = "https://github.com/cmpute/dashu.git", rev = "22f3935", default-
basm/src/solution.rs를 다음과 같이 수정합니다.

```rust
use basm::platform::io::{Reader, ReaderTrait, Writer};
use basm::platform::io::{Reader, ReaderTrait, Writer, Print};
use alloc::string::ToString;
use core::str::FromStr;
use dashu::Integer;
Expand All @@ -226,8 +226,7 @@ pub fn main() {
let a = Integer::from_str(&reader.word()).unwrap();
let b = Integer::from_str(&reader.word()).unwrap();
let ans = &a + &b;
writer.str(&ans.to_string());
writer.byte(b'\n');
writer.println(ans.to_string());
}
```

Expand Down Expand Up @@ -294,7 +293,7 @@ basm/src/solution.rs를 다음과 같이 수정합니다.


```rust
use basm::platform::io::{Reader, ReaderTrait, Writer};
use basm::platform::io::{Reader, ReaderTrait, Writer, Print};
use alloc::string::ToString;
use core::str::FromStr;
use dashu::Integer;
Expand All @@ -310,7 +309,6 @@ use nom::{
sequence::{delimited, pair},
};


fn number_literal(input: &str) -> IResult<&str, Integer> {
map_res(take_while(|x: char| is_digit(x as u8)), |s: &str| Integer::from_str(s))(input)
}
Expand Down Expand Up @@ -347,11 +345,10 @@ pub fn main() {
let mut writer: Writer = Default::default();
let input = reader.word();
if let Ok((_, ans)) = all_consuming(expr)(&input) {
writer.str(&ans.to_string());
writer.println(ans.to_string());
} else {
writer.str("ROCK");
}
writer.byte(b'\n');
writer.println("ROCK");
};
}
```

Expand Down Expand Up @@ -621,17 +618,18 @@ basm/src/solution.rs를 다음과 같이 수정합니다.
```rust
use alloc::{format, string::String, vec::Vec};
use basm::serialization::Pair;
use basm_macro::{basm_export, basm_import};
pub fn main() {
}

basm_macro::basm_import! {
basm_import! {
fn guess(b: String) -> Pair::<i32, i32>;
}

static mut ALL: Vec<i32> = Vec::new();
static mut N: i32 = 0;

#[basm_macro::basm_export]
#[basm_export]
fn init(_t: i32, n: i32) {
unsafe {
ALL.clear();
Expand Down Expand Up @@ -672,7 +670,7 @@ fn check(mut x: i32, mut y: i32) -> Pair::<i32, i32> {
Pair::<i32, i32>(strikes, balls)
}

#[basm_macro::basm_export]
#[basm_export]
fn game() {
let mut all = unsafe { ALL.clone() };
let n = unsafe { N };
Expand Down
19 changes: 19 additions & 0 deletions basm-std/src/platform/io/writer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::string::String;
use core::mem::MaybeUninit;
use crate::platform::services;

Expand Down Expand Up @@ -371,6 +372,24 @@ impl<const N: usize> Print<&str> for Writer<N> {
}
}

impl<const N: usize> Print<String> for Writer<N> {
fn print(&mut self, x: String) {
self.print(x.as_str());
}
fn println(&mut self, x: String) {
self.println(x.as_str());
}
}

impl<const N: usize> Print<&String> for Writer<N> {
fn print(&mut self, x: &String) {
self.print(x.as_str());
}
fn println(&mut self, x: &String) {
self.println(x.as_str());
}
}

macro_rules! impl_print{
($($ty:ident)*) => {
$(
Expand Down

0 comments on commit 98f2d0b

Please sign in to comment.