These are chat archives for rust-lang/rust

12th
Jan 2018
Ed Page
@epage
Jan 12 2018 01:51

Any tips, examples, etc for writing custom reference types? I need an owned version of my struct for most of my program but there are cases where a borrowed version would be nice. I can hack something together but would prefer doing it "first class" and am running into problems.

See https://play.rust-lang.org/?gist=565452d6b8b6276490e5de32966d84ba&version=stable for more details

TatriX
@TatriX
Jan 12 2018 07:41
@oyvindln the problem was that ggez used flate2 v1.0 and tiled was using v0.1. So I just updated tiled's Cargo.toml and the issue gone.
Sherab Giovannini
@Shaddy
Jan 12 2018 11:47
@TatriX I'm not probably the best to say this, but for me data.as_slice() is explicit, while &data[..] doesn't since you are not using any specific range.
dunnousername
@dunnousername
Jan 12 2018 20:29
Hey, I'm trying to use the nightly rust to make a less verbose assembly program; in other words, I'm removing libcore, std, and libc from everything, and using plain syscalls via inline assembly. I've run into a problem, however, where I want to write(2) "Hello World", but I cannot get the string as a pointer without an implementation of &str, and I'm trying to implement it in one page (4kB) and with minimal size.
Steve Klabnik
@steveklabnik
Jan 12 2018 20:30
why remove libcore?
dunnousername
@dunnousername
Jan 12 2018 20:31
Just to see how small I can make the executable, and just to play around and understand better
I don't need half the features it provides, and this could work without libc, for example. Maybe in a small initrd on linux, for example, this could be useful.
Steve Klabnik
@steveklabnik
Jan 12 2018 20:32
i would suggest you do it with libcore first, and then after try to change it
libcore does not require libc
dunnousername
@dunnousername
Jan 12 2018 20:32
Doesn't it require mem*?
Steve Klabnik
@steveklabnik
Jan 12 2018 20:32
memcpy, memcmp, memset only
https://crates.io/crates/rlibc implements just these in pure rust
it's like 80 loc
basically, you'll just end up re-implemeting libcore by removing it, IMO
people doing os dev, even on embedded devices, still use libcore as far as i know
anyway. just saying you might be making this way more painful for yourself than you have to
dunnousername
@dunnousername
Jan 12 2018 20:34
That might be a good idea. I don't need 128-bit floats, for example (I hear that is in libcore), can I selectively remove features?
Steve Klabnik
@steveklabnik
Jan 12 2018 20:35
my OS doesn't use floats at all; you end up using https://crates.io/crates/xargo and a target.json
dunnousername
@dunnousername
Jan 12 2018 20:36
Does it end up linking those features in the final object though?
Steve Klabnik
@steveklabnik
Jan 12 2018 20:37
usually stuff like gc-sections ends up stripping out everything you don't use
dunnousername
@dunnousername
Jan 12 2018 20:37
Oh, then using libcore makes a lot more sense now
Steve Klabnik
@steveklabnik
Jan 12 2018 20:38
:+1:
dunnousername
@dunnousername
Jan 12 2018 20:50
Do I need to clobber registers marked as inputs?
I get segfaults and weird things happening
fn syscall(callnum: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
    let ret: isize;

    unsafe {
        asm!(
          "syscall" 
        : "={rax}"(ret)
        : "{rax}"(callnum), "{rdi}"(arg1), "{rsi}"(arg2), "{rdx}"(arg3)
        : "rax"
        );
    }

    return ret;
}
dunnousername
@dunnousername
Jan 12 2018 20:56
Or maybe I'm using every register?
dunnousername
@dunnousername
Jan 12 2018 21:14
So, I've tried to execute using cdecl to test the function works, and when I do this NASM, it just loops forever. And, when I remove the jmp $, it segfaults. I'm not sure why this is happening, and there are no missing symbols in compilation
extern syscalll ; renamed to avoid conflict with instruction syscall

global _start

hello: db "Hello World!", 0xa
hello_len equ $ - hello
_start:
    push qword hello_len
    push qword hello
    push qword 1
    push qword 1
    call syscalll
    jmp $
Denis Lisov
@tanriol
Jan 12 2018 21:17
Have you specified the calling convention anywhere?
dunnousername
@dunnousername
Jan 12 2018 21:17
huh?
#[no_mangle]
pub extern "cdecl" fn syscalll(callnum: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
    let ret: isize;

    unsafe {
        asm!(
          "syscall" 
        : "={rax}"(ret)
        : "{rax}"(callnum), "{rdi}"(arg1), "{rsi}"(arg2), "{rdx}"(arg3)
        : "rax", "rdi", "rsi", "rdx"
        );
    }

    return ret;
}
I'm pretty sure syscall 1 is write, I hope that is correct. I've tried other syscalls and it still does nothing, however
huh, for some reason that tiny static library is 1MB